【问题标题】:Zend_PDF: Remove html contents from PDF documentZend_PDF:从 PDF 文档中删除 html 内容
【发布时间】:2012-08-30 05:48:17
【问题描述】:

我在自定义控制器中创建了函数 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 reader 无法打开 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内容?

【问题讨论】:

    标签: php zend-framework magento-1.7 magento-1.6 zend-pdf


    【解决方案1】:

    创建 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 后下载它。有关更多信息,请参阅How to make PDF file downloadable in HTML link?

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

    注意,这是一个快速而肮脏的 hack,滥用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 的下载链接。

    【讨论】:

    • 我已经尝试了如何使 PDF 文件在 HTML 链接中可下载?它不起作用也会引发相同的错误。我希望 PDF 文档应该自动保存在浏览器的下载文件夹中。您知道如何从 pdf 文档中删除 html 内容吗?
    • 您是否已经证明您在 服务器创建 PDF 文件的方式确实生成了一个正确 PDF 文件?您的服务器似乎在 Windows 下运行,因此双击您创建的 PDF 文件应该会告诉您文件是否正常,否则您的 Acrobat Reader 将无法打开它。如果它是一个正确的 PDF 文件,那么您的问题是关于 下载(而不是关于生成)PDF 文件。
    • 没错,问题是下载Pdf不生成Pdf。
    • 感谢您所做的努力,上面的代码在“app/code/core/Mage/Cms/controllers/IndexController.php”中运行良好......但我想从PDF中删除html内容我的自定义控制器中的文档不在“app/code/core/Mage/Cms/controllers/IndexController.php”文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2010-09-09
    • 2011-08-17
    • 2014-03-14
    • 2010-11-26
    相关资源
    最近更新 更多