3

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Nida
  • 1,672
  • 3
  • 35
  • 68

1 Answers1

2

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.

Community
  • 1
  • 1
Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
  • I have tried the solution specified in How to make PDF file downloadable in HTML link? It doesn't works also throws the same error. I want that PDF document should be automatically saved in downloads folder of the browser. Do you have any idea about how to remove html contents from pdf document? – Nida Aug 30 '12 at 09:17
  • Have you already proven that the way you _create_ the PDF file on your _server_ really produces a _proper_ PDF file? You're server seems to run under Windows, so double clicking the PDF file you've created should tell you, if the file is OK, since otherwise your Acrobat Reader will not be able to open it. If it's a proper PDF file, then your issue is about _downloading_ (not about generating) the PDF file. – Jürgen Thelen Aug 30 '12 at 09:38
  • Exactly, the issue is about downloading the Pdf not generating Pdf. – Nida Aug 30 '12 at 09:44
  • Thanks for the efforts you made and the above code works well in "app/code/core/Mage/Cms/controllers/IndexController.php"... But I want to remove html contents from PDF document in my custom controller not in "app/code/core/Mage/Cms/controllers/IndexController.php" file. – Nida Aug 31 '12 at 07:39