Question

I have created function getPdf in my custom controller

public function getPdf()
{ 
 $pdf = new Zend_Pdf(); 
 $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
 $imagePath="C:\Users\Hp\Desktop\landscape3.jpg";
 $image = Zend_Pdf_Image::imageWithPath($imagePath);
 $page->drawImage($image, 40,764,240, 820);
 $pdf->pages[] = $page;
 $pdf->save('myfile.pdf');
}

It generates PDF with image but in magento1.7 folder. I have tried the following lines

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

It generates PDF document in Downloads folder but when I open it.An error message is displayed:Adobe reader couldn't open myfile.pdf because it's not either a supported file type or because the file has been damaged............ When I open mydownloads.pdf in text editor (notepad), I notice presence of html contents(the contents of current phtml file in which getPdf() function is called) with pdf's own contents.I even tried to disable view rendering in magento1.7 by using

$vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); 
$vr->setNoRender(true); 
$layout = Zend_Layout::getMvcInstance();
$layout->disableLayout();

but unfortunately it didn't work in magento1.7.Then I tried to echo the contents of $pdfString by removing those two lines of header function
like

 $pdfString = $pdf->render();
 return  $pdfString;

It simply contained Pdf's own contents. Then I realised that presence of html contents is due to these two lines

header("Content-Disposition: attachment; filename=mydownloads.pdf");
header("Content-type: application/x-pdf"); 

Can anybody help me in resolving this issue. So that PDF document is generated in download folder. How to remove html contents from pdf document?

Was it helpful?

Solution

Creating the PDF file

To create the PDF document in a server folder of your choice, you could use something like this:

$sImagePath = 'C:\Users\Hp\Desktop\landscape3.jpg';
$sPdfPath = 'C:\Users\Hp\Desktop\my.pdf';

$oPdf = new Zend_Pdf();
$oPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$oImg = Zend_Pdf_Image::imageWithPath($sImagePath);
$oPage->drawImage($oImg, 40, 764, 240, 820);
$oPdf->pages[] = $oPage;
$sContent = $oPdf->render();
file_put_contents($sPdfPath, $sContent);

Providing a download link

It seems that you also want to provide a link for your users, so that they can download the PDF, once it has been created. See How to make PDF file downloadable in HTML link? for more on this.

Example of creating and downloading using a controller

Note, this is a quick and dirty hack, misusing the Mage_Cms_IndexController just to quickly prove and demonstrate the principle being used. Porting this to your custom controller should be a piece of cake, anyway.

Tested and working fine on a fresh Magento 1.7.0.2 instance and using FF15 and IE9:

class Mage_Cms_IndexController extends Mage_Core_Controller_Front_Action
{

    const PDF_PATH = '/home/user/public_html/magento-1-7-0-2/';

    public function indexAction($coreRoute = null)
    {

        // Create the PDF file
        $sImagePath = '/home/user/public_html/magento-1-7-0-2/media/catalog/category/furniture.jpg';
        $sPdfPath = self::PDF_PATH . 'myfurniture.pdf';
        $oPdf = new Zend_Pdf();
        $oPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
        $oImg = Zend_Pdf_Image::imageWithPath($sImagePath);
        $oPage->drawImage($oImg, 40, 764, 240, 820);
        $oPdf->pages[] = $oPage;
        $sContent = $oPdf->render();
        file_put_contents($sPdfPath, $sContent);

        // Echo the download link
        echo '<a href="cms/index/download/pdf/myfurniture">Download myfurniture.pdf</a>';
        return;

    /*
        $pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE);
        if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
            $this->_forward('defaultIndex');
        }
    */

    }

    public function downloadAction()
    {

        // Cancel if the `pdf` param is missing
        if (!$sPdfName = $this->getRequest()->getParam('pdf', false)) {
            return $this->_forward('noRoute');
        }

        // Sanitize passed value and form path
        $sPdfName = preg_replace('[a-z0-9]', '', $sPdfName);
        $sPdfPath = self::PDF_PATH . $sPdfName . '.pdf';

        // Cancel if file doesn't exist or is unreadable
        if (!file_exists($sPdfPath) || !is_readable($sPdfPath)) {
            return $this->_forward('noRoute');
        }

        // Set proper headers
        header('Content-Type: application/pdf');
        header("Content-Disposition: attachment; filename=" . urlencode($sPdfName . '.pdf'));
        header('Content-Transfer-Encoding: binary');
        header("Content-Length: " . filesize($sPdfPath));

        // Send
        readfile($sPdfPath);

    }

    // :

}

Just temporarily copy/paste this to your

app/code/core/Mage/Cms/controllers/IndexController.php

file, adjust the paths, and start testing.

Loading the homepage of your Magento installation should show you a download link for the PDF then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top