Question

In magento1.7, I have tried something like below in my custom controller.

public function getPDF()
{
$imagePath=C:\Users\.........;
$image = Zend_Pdf_Image::imageWithPath($imagePath);
$page->drawImage($image, 40,764,240, 820);
.
.
.
$pdf->pages[] = $page;
$pdf->save("mydoc.pdf");
}

There's no error in it. It generates PDF with image but the PDF document is saved in magento folder instead in My downloads folder. After doing some research, I found some following chunk of lines and added them after $pdf->pages[] = $page;.

  $pdfString = $pdf->render();
  header("Content-Disposition: attachment; filename=myfile.pdf");
  header("Content-type: application/x-pdf");
  echo $pdfString;

Now it generates PDF in My Downloads folder. When I try to open it. It throws error saying : Adobe reader couldn't open myfile.pdf because it's not either a supported file type or because the file has been damaged............ Do this happens,when we try to open PDF document generated on localhost or there's some other reason. Please let me know, why this error occurs and also provide me a solution to resolve it.

Was it helpful?

Solution

your problem is probably due to calling both save() and render() together.

save() actually calls render(), the issue could be due to trying to render the PDF twice.

This is also a waste of resources, if you need to save the file it's probably best to just save the file first, and then serve this file directly to the user.

You can do this in plain old PHP (using passthru or readfile), although there's ways to do this inside Zendframework which are better you can look into :)

// .. create PDF here.. 
$pdf->save("mydoc.pdf");

$file = 'mydoc.pdf';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

if your code is inside a Magento Controller:

    $this->getResponse()
        ->setHttpResponseCode(200)
        ->setHeader('Pragma', 'public', true)
        ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
        ->setHeader('Content-type', $contentType, true)
        ->setHeader('Content-Length', filesize($file))
        ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"')
        ->setHeader('Last-Modified', date('r'));

    $this->getResponse()->clearBody();
    $this->getResponse()->sendHeaders();

    $ioAdapter = new Varien_Io_File();
    if (!$ioAdapter->fileExists($file)) {
        Mage::throwException(Mage::helper('core')->__('File not found'));
    }
    $ioAdapter->open(array('path' => $ioAdapter->dirname($file)));
    $ioAdapter->streamOpen($file, 'r');
    while ($buffer = $ioAdapter->streamRead()) {
        print $buffer;
    }
    $ioAdapter->streamClose();
    exit(0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top