Zend_PDF:从PDF文档中删除html内容


Zend_PDF: Remove html contents from PDF document

我在自定义控制器中创建了函数getPdf

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');
}

它生成带有图像的PDF,但位于magento1.7文件夹中。我试过以下线路

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

它在下载文件夹中生成PDF文档,但当我打开它时。会显示一条错误消息:Adobe阅读器无法打开myfile.PDF,因为它不是受支持的文件类型,或者因为文件已损坏。。。。。。。。。。。。当我在文本编辑器(记事本)中打开mydownloads.pdf时,我注意到存在html内容(调用getPdf()函数的当前phtml文件的内容)和pdf自己的内容。我甚至试图通过使用禁用magento1.7中的视图渲染

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

但不幸的是,它在magento1.7.中不起作用。然后,我试图通过删除头函数的这两行来回显$pdfString的内容

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

它只是包含了Pdf自己的内容。然后我意识到html内容的存在是由于这两行

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

有人能帮我解决这个问题吗。这样PDF文档就在下载文件夹中生成了。如何从pdf文档中删除html内容?

创建PDF文件

要在您选择的服务器文件夹中创建PDF文档,您可以使用以下内容:

$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);

提供下载链接

你似乎还想为用户提供一个链接,这样他们就可以在创建PDF后下载。请参阅如何在HTML链接中下载PDF文件?了解更多信息。

使用控制器创建和下载的示例

注意,这是一个快速而肮脏的破解,滥用Mage_Cms_IndexController只是为了快速证明和演示所使用的原理。无论如何,将其移植到您的自定义控制器应该是小菜一碟。

在新的Magento 1.7.0.2实例上测试并运行良好,并使用FF15和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);
    }
    // :
}

只是暂时复制/粘贴到你的

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

文件,调整路径,然后开始测试。

加载Magento安装的主页应该会显示PDF的下载链接。