How to display a pdf file

Login or register to post comments
4 replies [Last post]
LaCeja's picture
Offline
Joined: 11/04/2009

I've been working on an interface to use the JavaBridge to call methods of Jasper iReport to run a report and place it in a pdf file. I've been successful with that. I have been able to run the report, creating the pdf file and it opens in Adobe Reader fine, when I double click on that file.

Now, I need to be able to send that file to the users browser. I don't want the user to have to press another button to display it. I just want to be able to display it, after the report has been created.

Can anyone help with this?

Thanks,

LaCeja

Offline
Joined: 02/02/2010

i would create a panel with a member variable that contains the filename of the pdf.

if (no filename is set --> render nothing (only wrapper div)
else <embed src="NAME_OF_FILE.pdf" width="500" height="375">

after creating and safing your pdf:
set the member var of your pdfViewPanel
call $myPdfViewPanel->MarkAsModified();

LaCeja's picture
Offline
Joined: 11/04/2009

Mike, I tried your suggestion and get no output at all. The panel is completely empty. I am certain the pdf file has been created. In the file system, I double click on it and it opens with the expected report.

I have also tried this in my code, but no file gets downloaded:

header("Pragma: no-cache");
header("Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
header('Cache-Control: private');
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=$filename");
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($outputPath) );
readfile($outputPath);

I have verified that the file has been created and all $outputPath variable does indeed contain the full path to the file.

It appears that QCubed is blocking the output, because I created a simple PHP script file and when I run it from the browser directly, it downloads the file as expected.

I've also looked at the code from the DataGridExporterButton plugin, which does a download of CSV files from a DataGrid and my code is very similar, but doesn't seem to work.

Help!

BTW, I have managed to integrate my QCubed application with the JavaBridge to create Jasper reports from the iReport code. It works nicely and runs pretty fast, since it runs with the compiled Jasper Report. All that's needed is to get the download part working. When that's done, I'll provide all the code for the community.

Thanks.

Offline
Joined: 02/02/2010

Hi i have created a test control that works for me with ajax and server actions:
if you use it you should set the width and height properties via the control's properties and you could make the file-type a member variable to make it useable for other file types too (i hardcoded them because i was too lazy)

i use a path relative to __DOCROOT__ for my file path
something like that './pictures/myPdf.pdf'

<?php

class QEmbeddedObject extends QControl {

    protected
$strFile;
    public function
__construct($objParentObject,$strControlId = null) {
         try {
           
parent::__construct($objParentObject, $strControlId);
         } catch (
QCallerException $objExc) { $objExc->IncrementOffset(); throw $objExc; }

        
$strFile="";
       
// $this->blnIsBlockElement=true;
    
}

     public function
__get($strName) {
    switch (
$strName) {
   
// APPEARANCE
     
case "File": return $this->strFile;
      default:
        try {
          return
parent::__get($strName);
        } catch (
QCallerException $objExc) {
         
$objExc->IncrementOffset();
          throw
$objExc;
        }
    }
  }

  public function
__set($strName, $mixValue) {
       
$this->blnModified = true;
        switch (
$strName) {
                    case
"File" :
           
$this->strFile=$mixValue;
                       
$this->MarkAsModified();
                        break;
                    default:
                        try {
                           
parent::__set($strName, $mixValue);
            }
                            catch (
QCallerException $objExc) {
                               
$objExc->IncrementOffset();
                throw
$objExc;
                            }
            break;
                }


  }

  protected function
GetControlHtml() {

        if(
$this->strFile == NULL || $this->strFile=="")
                return
"";

       
// Setup Style
       
$strStyle = $this->GetStyleAttributes();
        if (
$strStyle)
         
$strStyle = sprintf('style="%s"', $strStyle);

       
       
$strToReturn = sprintf('<object id="%s" type="application/pdf" data="%s" width="500" height="650" ></object>',
           
$this->strControlId,
           
//$this->GetAttributes(),
           // $strStyle,
           
$this->strFile);

        return
$strToReturn;
  }

  public function
ParsePostData() {
  }

  public function
Validate(){
      return
true;
  }

}
?>

LaCeja's picture
Offline
Joined: 11/04/2009

Mike, I really appreciate the code. However, I have decided it would indeed be better to serve up the file as a download to the client, because they may very well want to save the file on their local system for reprinting later.

I'm having a real problem with that download function as well and have started a new thread for it here.

Again, thank you.