3

カスタムコントローラーに関数 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;

Downloads フォルダーに 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では機能しませんでした。次に、次のようなヘッダー関数の2行を削除して、$ pdfStringの内容をエコーし​​ようとしました

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

単に Pdf 独自のコンテンツが含まれていました。次に、html コンテンツの存在はこの 2 行によるものであることに気付きました

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

誰でもこの問題を解決するのを手伝ってくれますか? そのため、ダウンロードフォルダーにPDFドキュメントが生成されます。PDFドキュメントからhtmlコンテンツを削除するには?

4

1 に答える 1

2

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 のダウンロード リンクが表示されます。

于 2012-08-30T08:44:36.263 に答える