dswsl2016 2013-12-13 09:46
浏览 51

用magento中的tcpdf创建一个pdf

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."
", 1, 'J', 1, 1, 60, 102, true);
$pdf->MultiCell(80, 5, $from."
", 1, 'J', 1, 1, 60, 123, true);
$pdf->MultiCell(80, 5, $currency."
", 1, 'J', 1, 1, 60, 145, true);
$pdf->MultiCell(80, 5, $code."
", 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条回答 默认 最新

  • dongqianchi0512 2016-11-29 10:51
    关注

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题