在magento中用tcpdf创建一个pdf


create a pdf with tcpdf in magento

我正在尝试在magento中使用TCPDF创建pdf,我需要导入一个图像或pdf并对其进行修改。我在firefox中的代码运行良好,但在chrome或internet中不起作用。

我放了一点代码:

我的控制器:

  public function printAction()
{
    if (($cardCode = $this->getRequest()->getParam('code'))) {
        $this->loadLayout('print');
        $this->getResponse()->clearHeaders()
                            ->setHeader('Content-Type', 'application/pdf');
        $this->renderLayout();
    } else {
        $this->_redirect('/');
    }
}

我的phtml:

require_once('tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8',       false);
$pdf->SetFont('helvetica', '', 15);
$pdf->AddPage();
$pdf->setJPEGQuality(100);
$fileName = Mage::getConfig()->getOptions()->getMediaDir()."/pdf/Gutschein-v2.jpg";
$pdf->Image($fileName, 0, 0, 210, 266, 'JPG', '', '', true, 150, '', false, false, 1,  false, false, false);
if ($giftCard->getMailTo()) { 
$name = $this->helper('core')->escapeHtml($giftCard->getMailTo(), null);
}
if ($giftCard->getMailFrom()) { 
$from = $this->helper('core')->escapeHtml($giftCard->getMailFrom(), null);
}
$code=$giftCard->getCardCode();
$currency= Mage::helper('core')->currency($giftCard->getCardAmount(), true, false);
$pdf->MultiCell(80, 5, $name."'n", 1, 'J', 1, 1, 60, 102, true);
$pdf->MultiCell(80, 5, $from."'n", 1, 'J', 1, 1, 60, 123, true);
$pdf->MultiCell(80, 5, $currency."'n", 1, 'J', 1, 1, 60, 145, true);
$pdf->MultiCell(80, 5, $code."'n", 1, 'J', 1, 1, 60, 166, true);
$pdf->lastPage();
ob_start();
$pdf->Output('example_009.pdf', 'I');
ob_end_flush();

我可以在firefox中完美地看到pdf,但不能从电脑上下载并打开它。错误:pdf已损坏。

在互联网和谷歌铬我看不到pdf。

我不知道我做错了什么。非常感谢。

您可以通过以下步骤轻松地在magento中添加tcpdf。

步骤1:从这里下载最新的tcpdf

步骤2:在magento根目录中的magento的lib目录中创建一个目录TCPDF(路径类似/lib/TCPDF)。(如果不允许创建目录,则从cPanel创建)

步骤3:在目录&将tcpdf.php重命名为tcpdf.php

步骤4:复制tcpdf_autoconfig.php文件。同时复制字体&打开TCPDF.php并将类名更改为类TCPDF_TCPDF{文件夹

步骤5:打开TCPDF.php并将类名更改为类TCPDF_TCPDF{

步骤6:使用以下样本代码进行测试:

$tcpdf = new TCPDF_TCPDF();
//your htmls here
$html = '<h1> hello world </h1>';
$tcpdf->AddPage();
$tcpdf->writeHTML($html, true, false, true, false, '');
$tcpdf->lastPage();
$tcpdf->Output('report_per_route_'.time().'.pdf', 'I');

OR将pdf文件保存在var/report

$fn = Mage::getBaseDir('base').'/var/report/report_'.time().'.pdf';
$tcpdf->Output($fn, 'F');