【问题标题】:How to print to pdf using php with a canvas element如何使用带有canvas元素的php打印到pdf
【发布时间】:2011-03-18 15:12:00
【问题描述】:

我正在尝试使用 PHP 制作动态 PDF 生成器,但到目前为止我遇到了一个问题,即我尝试生成为 PDF 的页面有一个由 flot 生成的条形图帆布。

有没有人有这方面的经验或知道如何将画布打印成 pdf?

我也愿意让我的客户加载一个页面,该页面使用 CSS 设置分页符样式,并使用内置的 Print to PDF 打印机,将报表的每个图表都按(打印)页面打破。

p>

【问题讨论】:

  • 您测试了哪种 PDF 打印机?我对 TCPDF 有过一些非常好的经验。
  • 到目前为止,我已经能够使用安装在 windows xp 机器上的任何默认 pdf 打印机打印整个网页。我会看看你的建议并在今晚进行测试。谢谢。

标签: php jquery pdf canvas pdf-generation


【解决方案1】:
//Here is the way to generate html into canvas into PDF

1 add can js files
jquery.js
JS/html2canvas.js
JS/jquery.plugin.html2canvas.js

2.get the html content to convert into canvas
        $('.hideDivs').hide(); 
        $('.formdrop').hide();
        $('.block').html2canvas({
            onrendered: function (canvas) {
                 //Set hidden field's value to image data (base-64 string)
                $('#img_val').val(canvas.toDataURL("image/jpg"));
                 $('#img_val3').val($('.block').html()); //html of div
                //Submit the form manualy

            }
        });

3. PHP back end with TC PDF
if(isset($_REQUEST['img_val'])):
        //Get the base-64 string from data
        $filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
        //Decode the string
        $unencodedData=base64_decode($filteredData);
        //Save the image
        $imgfile=FCPATH.'images/submsnimage_'.$this->session->userdata('admin_id').'.jpg';
        $imagegerated=file_put_contents($imgfile, $unencodedData);
        chmod($imgfile, 0777);
        $pdfImageFile=site_url()."images/submsnimage_".$this->session->userdata('admin_id').'.jpg';
endif;

4 PDF generation
        ini_set('max_execution_time',300);
        require_once (FCPATH.'tcpdf/config/lang/eng.php');
        require_once (FCPATH.'tcpdf/tcpdf.php');
        $exa = new TCPDF ();
        $exa->SetCreator ( PDF_CREATOR );
        $exa->SetAuthor ( 'Clay County Admin' );
        $exa->SetTitle ( 'Clay County Form' );
        $exa->SetSubject ( 'Example of TCPDF' );
        $exa->SetKeywords ( 'TCPDF, PDF, PHP' );
        $exa->SetFont ( 'times', '', 18 ); 
        $exa->AddPage ();
        $exa->Image ($pdfImageFile, 15, 10, 430,630); 
        $exa->AddPage ();
        $exa->Image ($pdfImageFile2, 15, 10, 430,630); 
        $exa->AddPage ();
        $exa->Image ($pdfImageFile3, 15, 10, 430,630); 

        $exa->setImageScale(PDF_IMAGE_SCALE_RATIO);
        $exa->setJPEGQuality (100);
        $root=site_url();
        $exa->setImageScale(1.53);

        $txt ="";
        $exa->WriteHTML ($txt, true, false, true, false, '');
        $exa->Output (FCPATH.'uploads/pdf/clayCommunityBullyReport.pdf', 'F' ); // downloads the pdf

【讨论】:

    【解决方案2】:

    我从未使用过<canvas> 来尝试创建/打印 pdf。我建议 NOT 使用<canvas>,因为并非所有浏览器都支持它。但是画布中有一个名为:toDataURL() 的函数,它将画布显示的内容转换为图像,然后您可以使用该图像进行保存/打印,但是它将是图像格式而不是 pdf。

    我建议使用 php 库创建一个 pdf,然后将其显示给用户。我个人使用过 FPDF,但这里列出了一些:

    http://www.fpdf.org/

    http://www.tcpdf.org/

    http://www.php.net/manual/en/book.pdf.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 2012-06-04
      • 2016-01-14
      相关资源
      最近更新 更多