Zend_pdf errors if opening in browser

718 Views Asked by At

I have an interesting problem. I tried around with Zend_Pdf. It formerly worked quite fine. But now since 2 days it doesn't work anymore. After starting my action I get the browser question open or save. If I choose open, I get an error, which says "cannot opened because file is damaged:

I deleted all stuff out of my controlleraction and tried with a simple version, but I get the same error. Here is my Controller action:

public function indexAction()
{
    $pdf = new Zend_Pdf();
    $pdf->properties['Title']='testtitle';
    //echo $pdf->properties['Title'] . "\n";
    $pdf->pages = array_reverse($pdf->pages);
    $style = new Zend_Pdf_Style();
    $style->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 36);
    $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
    $pdf->pages[1] = ($page = $pdf->newPage('A4'));
    $page=$pdf->pages[1];
    $page->setFont($font, 18);
    $page->drawText("testcaption", 100, 100);
    header("Content-Type: application/pdf");
    header("Content-Disposition: inline; filename=test.pdf");
    //header("Cache-Control: no-cache, must-revalidate");
    echo $pdf->render();
    // $fileName="./pdfs/test.pdf"; 
    // $pdf->save($fileName);   
}

I deleted everything because I thought my code was eventually not completely valid, but now I only have the test left.I want to remark that there was a firefox update, so I tried with IE, but same problem. Perhaps there is a basic mistake? Another remark, in this case I only work with the controller action, I don't have any viewscript. Do I need one because the controller builds the pdf? If yes what should be inside? Does anybody know the answer or some workaround?

new phänomenon: I changed my code (the last statements) to:

$fileName="./pdfs/test.pdf";    
$pdf->save($fileName);  
echo $pdf->render();

I get the same message in my browser. Now I have a copy on my webserver, if I open this file with doubleclick, it opens without any error..

I tried to choose "save under" in my browser and choosed some folder outside my webserver. This file I can't open, now I'm totally confused.

Somebody can help?

Update:

I tried some other possibilities. Very interesting is, that I have 2 different pdf-dokuments. The one I save (with save method) on my server opens fine, everything ok. If I force for the other one a download in my browser and open the document out of my download folder.

I get a document which has my html layout inside and also the pdf date. It really starts like follows:

<base href="/mosaik/public/" />

after that there are my div areas and at the end there are my pdf areas.

This is the same effect, but while trying I changed my code, I built a class which provides the pdf and I have a view, where I set my header:

<?php
$response = Zend_Controller_Front::getInstance()->getResponse();
//$response = new Zend_Controller_Response_Http();
 $response->setHeader('Content-Disposition', 'attachment; fileName=schwesternliste.pdf');
$response->setHeader('Content-type', 'application/pdf');

 $pdf=new Application_Model_Pdf_Schwestern();
 $pdf->load();
 $pdf->fillData();
echo $pdf->render();

?>

This must be something with the headers, somebody knows the solution?

1

There are 1 best solutions below

0
pia-sophie On

Because of the fact, this portal has been online since appr. 5 months and worked before very well, I tried another computer I have and on which I don't use automatik updates, and there it still works. Interesting! I tried a bit around and as I wrote above I don't have trouble to open the saved pdf, I only have these errors if I render it in my browser. I compared both files with eclipse and made out that the one which causes errors has my hole html layout in the header, the other one is clear pdf.

Because I have the same action for 3 different modules and I need it to be working, I made a workaround which is surely not the best solution but I had no other idea.

Now the controller action saves the pdf on the server and my view puts some headers on it and reopens it. This works fine. Here for everybody who has the same effect my view script:

<?php
require_once 'Zend/Pdf.php';

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename=schwesternliste.pdf');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression','0');
echo file_get_contents("./pdfs/schwesternliste.pdf");

?>
<html>
<body>