通过传递URL和$ _GET参数来旋转PDF?

时间:2022-03-10 21:10:30

I have spent a lot of time trying to get dompdf (http://www.digitaljunkies.ca/dompdf/) to work but I keep running into problems. I am trying to generate a PDF from a PHP script which generates a fairly complex, filled out web form. The script accepts a $_GET parameter (record number) and fills out the form accordingly with data from the database. I have no problem getting this data into the script as a string or any type of value really. What I am wondering is what the best approach would be for converting this type of data to a PDF?

我花了很多时间试图让dompdf(http://www.digitaljunkies.ca/dompdf/)工作,但我一直遇到问题。我试图从PHP脚本生成一个PDF,生成一个相当复杂,填写完整的Web表单。该脚本接受$ _GET参数(记录号),并使用数据库中的数据相应地填写表单。我将这些数据作为字符串或任何类型的值实际存入脚本都没有问题。我想知道将这种类型的数据转换为PDF的最佳方法是什么?

The flow is as follows: user completes form and is taken to confirmation page which I would like to add a "Save as PDF" button. At this point one of two things could happen, the page that is currently being displayed in the browser could be spun directly to a pdf or a call to itself (scriptname.php?id=xyz) could be made using something like PHP's http_get() function and store the HTML as a string. From there I am having issues with preparing an accurate representation as a PDF.

流程如下:用户完成表单并进入确认页面,我想添加“另存为PDF”按钮。此时,可能会发生以下两种情况之一:当前在浏览器中显示的页面可以直接转换为pdf,或者可以使用PHP的http_get等对其自身的调用(scriptname.php?id = xyz)进行调用( )函数并将HTML存储为字符串。从那里我遇到了准备PDF格式的精确表示的问题。

I have heard some talk about fpdf but their examples don't really lead me to believe you can use dynamic data as the source, but please correct me if I am wrong about this.

我听过一些关于fpdf的讨论,但他们的例子并没有让我相信你可以使用动态数据作为来源,但如果我错了,请纠正我。

Any input would be appreciated.

任何输入将不胜感激。

-- Nicholas

2 个解决方案

#1


Well, I didn't know dompdf. Strange that it uses either a commercial library (PDFlib) or an outdated (?) one (CPDF, not updated for 3 years). But well, as long as it works (concept is interesting).

好吧,我不知道dompdf。奇怪的是它使用商业图书馆(PDFlib)或过时的(?)(CPDF,未更新3年)。但是,只要它有效(概念很有趣)。

I don't understand what you mean by "use dynamic data as the source" (or rather, I see not point in generating static PDF!), but FPDF is used to generate various dynamic documents, like invoices in e-commerce products. I saw people using forks (like TCPDF) to handle Unicode data, though.

我不明白你的意思是“使用动态数据作为源”(或者更确切地说,我认为生成静态PDF没有意义!),但FPDF用于生成各种动态文档,如电子商务产品中的发票。不过,我看到人们使用forks(如TCPDF)来处理Unicode数据。

You can't transform an HTML page to PDF with FPDF, but you have a quite precise control of layout, using concept of cells with data.
You can see such kind of code there: http://svn.prestashop.com/trunk/classes/PDF.php

您无法使用FPDF将HTML页面转换为PDF,但您可以使用包含数据的单元格概念对布局进行非常精确的控制。你可以在那里看到这样的代码:http://svn.prestashop.com/trunk/classes/PDF.php

#2


In the past when faced with this issue, I have used FPDF for the placement of data on a PDF template. Then, by setting the appropriate HTTP header, force the browser to pop open the Download / Save As box for the user to save said PDF.

在过去遇到此问题时,我使用FPDF在PDF模板上放置数据。然后,通过设置适当的HTTP标头,强制浏览器弹出下载/另存为框,以便用户保存所述PDF。

In a class that extends FPDF/FPDI appropriately, use something like the following to generate a PDF from a template PDF you've already created (http://www.setasign.de/products/pdf-php-solutions/fpdi/):

在适当扩展FPDF / FPDI的类中,使用类似下面的内容从您已创建的模板PDF生成PDF(http://www.setasign.de/products/pdf-php-solutions/fpdi/) :

$this->setSourceFile('pdf_template.pdf');
$template_page = $this->importPage(1, '/MediaBox');
$this->useTemplate($template_page, 0, 0);

Then, have FPDF generate the PDF for output using:

然后,让FPDF生成用于输出的PDF:

$this->Output();

You can also extend FPDF to accept (limited) HTML for formatting using the script found here: http://www.fpdf.org/en/script/script41.php

您还可以使用以下脚本扩展FPDF以接受(有限的)HTML格式:http://www.fpdf.org/en/script/script41.php

#1


Well, I didn't know dompdf. Strange that it uses either a commercial library (PDFlib) or an outdated (?) one (CPDF, not updated for 3 years). But well, as long as it works (concept is interesting).

好吧,我不知道dompdf。奇怪的是它使用商业图书馆(PDFlib)或过时的(?)(CPDF,未更新3年)。但是,只要它有效(概念很有趣)。

I don't understand what you mean by "use dynamic data as the source" (or rather, I see not point in generating static PDF!), but FPDF is used to generate various dynamic documents, like invoices in e-commerce products. I saw people using forks (like TCPDF) to handle Unicode data, though.

我不明白你的意思是“使用动态数据作为源”(或者更确切地说,我认为生成静态PDF没有意义!),但FPDF用于生成各种动态文档,如电子商务产品中的发票。不过,我看到人们使用forks(如TCPDF)来处理Unicode数据。

You can't transform an HTML page to PDF with FPDF, but you have a quite precise control of layout, using concept of cells with data.
You can see such kind of code there: http://svn.prestashop.com/trunk/classes/PDF.php

您无法使用FPDF将HTML页面转换为PDF,但您可以使用包含数据的单元格概念对布局进行非常精确的控制。你可以在那里看到这样的代码:http://svn.prestashop.com/trunk/classes/PDF.php

#2


In the past when faced with this issue, I have used FPDF for the placement of data on a PDF template. Then, by setting the appropriate HTTP header, force the browser to pop open the Download / Save As box for the user to save said PDF.

在过去遇到此问题时,我使用FPDF在PDF模板上放置数据。然后,通过设置适当的HTTP标头,强制浏览器弹出下载/另存为框,以便用户保存所述PDF。

In a class that extends FPDF/FPDI appropriately, use something like the following to generate a PDF from a template PDF you've already created (http://www.setasign.de/products/pdf-php-solutions/fpdi/):

在适当扩展FPDF / FPDI的类中,使用类似下面的内容从您已创建的模板PDF生成PDF(http://www.setasign.de/products/pdf-php-solutions/fpdi/) :

$this->setSourceFile('pdf_template.pdf');
$template_page = $this->importPage(1, '/MediaBox');
$this->useTemplate($template_page, 0, 0);

Then, have FPDF generate the PDF for output using:

然后,让FPDF生成用于输出的PDF:

$this->Output();

You can also extend FPDF to accept (limited) HTML for formatting using the script found here: http://www.fpdf.org/en/script/script41.php

您还可以使用以下脚本扩展FPDF以接受(有限的)HTML格式:http://www.fpdf.org/en/script/script41.php