CSS 如何将 HTML 表单数据输出为 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9412224/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:04:21  来源:igfitidea点击:

How do I output HTML form data to PDF?

cssformspdf

提问by WebDevKev

I need to collect data from a visitor in an HTML form and then have them print a document with the appropriate fields pre-populated. They'll need to have a couple of signatures on the document, so it has to be printed.

我需要在 HTML 表单中从访问者那里收集数据,然后让他们打印一个预先填充了适当字段的文档。他们需要在文件上有几个签名,所以必须打印出来。

The paper form already exists, so one idea was to scan it in, with nothing filled out, as an image. I would then have the HTML form data print out using CSS for positioning and using the blank scanned form as a background image.

纸质表格已经存在,所以一个想法是将其扫描进来,不填写任何内容,作为图像。然后,我将使用 CSS 打印出 HTML 表单数据以进行定位并使用空白扫描表单作为背景图像。

A better option, I would think, would be to automatically generate the PDF with this data, but I'm not sure how to accomplish either.

我认为更好的选择是使用这些数据自动生成 PDF,但我不确定如何完成。

Suggestions and ideas would be greatly appreciated! =)

建议和想法将不胜感激!=)

采纳答案by Christopher

I would have to respectfully disagree with Osvaldo. Using CSS to align on a printed document would take ages to do efficiently in the aspect of cross-browser integration. Plus, if Microsoft comes out with a new browser, you're going to have to constantly update for the new use in browsers.

我不得不恭敬地不同意奥斯瓦尔多。在跨浏览器集成方面,使用 CSS 在打印文档上对齐需要很长时间才能有效地完成。另外,如果微软推出新的浏览器,你将不得不不断更新以适应浏览器的新用途。

If you know any PHP (Which, if you know JavaScript and HTML, basic PHP is very simple), here's a good library you can use, FDPF:

如果你知道任何 PHP(如果你知道 JavaScript 和 HTML,基本的 PHP 非常简单),这里有一个很好的库,你可以使用,FDPF

Thankfully, PHP doesn't deprecate a whole lot of methods and the total code is less than 10 lines if you have to go in and change things around.

值得庆幸的是,PHP 并没有弃用很多方法,而且如果您必须进行更改,那么总代码不到 10 行。

回答by Osvaldo

You can control printed documents acceptably well with CSS, so I would suggest you to try that option first. Because it's easier.

您可以使用 CSS 很好地控制打印文档,因此我建议您先尝试该选项。因为它更容易。

回答by Anagio

This is actually a great php library for converting HTML to PDF documents http://code.google.com/p/dompdf/there are many demo's available on the site

这实际上是一个很棒的 php 库,用于将 HTML 转换为 PDF 文档http://code.google.com/p/dompdf/ 网站上有许多演示可用

回答by xtrem

XSL-FOis what I would recommend. XSL-FO (along with XSLT and XPath) is a sub-standard of XSL that was designed to be an abstract representation of a formatted document (that contains, text, graphic elements, fonts, styles, etc).

XSL-FO是我推荐的。XSL-FO(连同 XSLT 和 XPath)是 XSL 的子标准,它被设计为格式化文档(包含文本、图形元素、字体、样式等)的抽象表示。

XSL-FO documents are valid xml documents, and there exist tools and apis that allow you to convert an XSL-FO documet to MS Word, PDF, RTF, etc. Depending on the technology you use, a quick google search will tell you what is available.

XSL-FO 文档是有效的 xml 文档,并且存在允许您将 XSL-FO 文档转换为 MS Word、PDF、RTF 等的工具和 API。根据您使用的技术,快速谷歌搜索会告诉您什么可用。

Here are a few links to help you get started with XSL-FO:

以下是一些可帮助您开始使用 XSL-FO 的链接: