使用 PHP 将 HTML 表单数据转换为 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12010110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert HTML form data into a PDF file using PHP
提问by DHymansway
I have been looking and testing this for a couple days now and was wondering if anyone could point me in a different direction. I have a very long job application HTML form (jobapp.html) and a matching PDF (jobpdf.pdf) that have the same field names for all entries in both the HTML form and the PDF. I need to take the user data that is entered in the form and convert it to a PDF. This is what I have gathered so far but don't know if I am on track:
我一直在寻找和测试这个几天,想知道是否有人可以指出我不同的方向。我有一个很长的工作申请 HTML 表单 (jobapp.html) 和一个匹配的 PDF (jobpdf.pdf),它们在 HTML 表单和 PDF 中的所有条目都具有相同的字段名称。我需要获取在表单中输入的用户数据并将其转换为 PDF。这是我到目前为止收集的内容,但不知道我是否走上正轨:
Is pdftk the only viable 3rd party app to accomplish this?
pdftk 是唯一可行的第 3 方应用程序吗?
Using pdftk would i take the $_POST data collected for the user and generate a .fdf(user.fdf) then flatten the .fdf on the .pdf(job.pdf). So irreguarless of where the fields are located on each document the information on the fdf would populate the pdf by field names?
使用 pdftk 我会为用户收集 $_POST 数据并生成 .fdf(user.fdf) 然后在 .pdf(job.pdf) 上展平 .fdf。因此,无论字段位于每个文档的哪个位置,fdf 上的信息都会按字段名称填充 pdf?
I have been trying http://koivi.com/fill-pdf-form-fields/tutorial.php
我一直在尝试 http://koivi.com/fill-pdf-form-fields/tutorial.php
I have also looked at "Submit HTML form to PDF"
我也看过“将HTML 表单提交到 PDF”
回答by Igor Parra
I have used fpdfseveral times to create php-based pdf documents. An example following:
我曾多次使用fpdf创建基于 php 的 pdf 文档。一个例子如下:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddFont('georgia', '', 'georgia.php');
$pdf->AddFont('georgia', 'B', 'georgiab.php');
$pdf->AddFont('georgia', 'I', 'georgiai.php');
# Add UTF-8 support (only add a Unicode font)
$pdf->AddFont('freesans', '', 'freesans.php', true);
$pdf->SetFont('freesans', '', 12);
$pdf->SetTitle('My title');
$pdf->SetAuthor('My author');
$pdf->SetDisplayMode('fullpage', 'single');
$pdf->SetLeftMargin(20);
$pdf->SetRightMargin(20);
$pdf->AddPage();
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
You can learn very fast with these tutorialsfrom the website itself.
您可以通过网站本身的这些教程快速学习。
EDIT: Example to save form data: (yes, is very easy...)
编辑:保存表单数据的示例:(是的,很容易......)
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
foreach ($_POST as $key =>$data)
{
$pdf->Write(5, "$key: $data"); //write
$pdf->Ln(10); // new line
}
$pdf->Output($path_to_file . 'file.txt','F'); // save to file
Look at these pages created with fpdf, really!
看看这些用 fpdf 创建的页面,真的!




回答by HandiworkNYC.com
That would be the library to do it. I used it here to add images to a form and submit it to create a PDF with those images: http://productionlocations.com/locations
那将是图书馆来做到这一点。我在这里使用它向表单添加图像并提交它以使用这些图像创建 PDF:http: //productionlocations.com/locations
The actual code to do it is pretty complex.
执行此操作的实际代码非常复杂。
回答by Phil
回答by Cyril N.
One way you can consider is using an online API that converts any HTML to PDF. You can send them a generated HTML (easier to produce) that will contains your user's submitted data, and receive back a high fidelity PDF.
您可以考虑的一种方法是使用在线 API 将任何 HTML 转换为 PDF。您可以向他们发送包含用户提交数据的生成的 HTML(更容易生成),并接收高保真 PDF。
There are quite a few services available on the market. I like to mention PDFShiftbecause it offers a package in PHPthat simplifies the work for you.
市场上有很多可用的服务。我喜欢提到PDFShift,因为它提供了一个PHP 包,可以简化您的工作。
Once you've installed it (using Composer, or downloaded it directly, depending on your choices) you can quickly convert an HTML document like this:
安装后(使用 Composer 或直接下载,具体取决于您的选择),您可以像这样快速转换 HTML 文档:
require_once('vendor/autoload.php');
use \PDFShift\PDFShift;
PDFShift::setApiKey('{your api key}');
PDFShift::convertTo('https://link/to/your/html', null, 'invoice.pdf');
And that's it. There are quite a few features you can implement (accessing secured documents, adding a watermark, and more).
就是这样。您可以实现很多功能(访问安全文档、添加水印等)。
Hope that helps!
希望有帮助!

