create a pdf with tcpdf in magento

狂风中的少年 提交于 2019-12-11 05:43:11

问题


I'm trying to create a pdf with TCPDF in magento, i need to import a image or a pdf and modify it. my code in firefox works good but doesn't work in chrome or in internet.

i put a little of my code:

my controller:

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

My 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();

I can see the pdf perfectly in firefox but not download and open it from my computer. Error: pdf ist damaged.

In internet and google chrome i can't see the pdf.

I don't know waht i'm doing wrong. thank you.


回答1:


you can easily add tcpdf in magento by following steps.

Step 1: Download the latest tcpdf from here

Step 2: Create a directory TCPDF in magento’s lib directory in magento root (path like /lib/TCPDF). (if not permission to create directory then create it from cPanel)

Step 3: Copy tcpdf.php in directory & rename tcpdf.php to TCPDF.php

Step 4: Copy tcpdf_autoconfig.php file. Also copy fonts & open TCPDF.php and change class name to class TCPDF_TCPDF { folder

Step 5: Open TCPDF.php and change class name to class TCPDF_TCPDF {

Step 6: Use the below sample code for testing:

$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 Save pdf file in var/report

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


来源:https://stackoverflow.com/questions/20563370/create-a-pdf-with-tcpdf-in-magento

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!