简体   繁体   English

服务器端的HTML到pdf转换?

[英]Html to pdf conversion at server side?

we are creating a pdf file from the html file using the package princexml pdf converter. 我们正在使用包princexml pdf转换器从html文件创建一个pdf文件。 For the Creation of the html file the data has given by the server. 对于创建html文件,数据由服务器提供。 In the browser using jquery the input string(html code) is created for the pdf creation. 在使用jquery的浏览器中,为pdf创建创建输入字符串(html代码)。 After receiving the input string from the browser, the server creates a html file which is the input to the princexml pdf converter for the pdf creation. 从浏览器接收到输入字符串后,服务器会创建一个html文件,该文件是princexml pdf转换器的输入,用于创建pdf。

Example for the input string
var sample = "<html>text</html>";//browser side
sample.html converted to sample.pdf //server side 

Is it possible to do the same thing at server side without help of the browser ? 没有浏览器的帮助,是否可以在服务器端执行相同的操作?

You can use a headless browser like http://phantomjs.org/ . 您可以使用像http://phantomjs.org/这样的无头浏览器。 This allows to generate images from rendered pages. 这允许从渲染页面生成图像。 See also http://www.lelesys.com/en/media/technology/phantomjs-as-screen-capture-to-generate-image-pdf-files.html 另见http://www.lelesys.com/en/media/technology/phantomjs-as-screen-capture-to-generate-image-pdf-files.html

This allows you to use jquery and everything else - since it uses the acutal rendering engine. 这允许您使用jquery和其他所有东西 - 因为它使用了实际渲染引擎。 I guess you do not even need princexml then. 我猜你甚至不需要princexml。 There is also a faq page regarding capture: http://phantomjs.org/screen-capture.html 还有一个关于捕获的常见问题解答页面: http//phantomjs.org/screen-capture.html

查看wkhtmltopdf - 使用Webkit(QtWebKit)将HTML转换为PDF @ http://wkhtmltopdf.org

I know this is a bit old question but since I found a great module I thought of sharing it with everyone. 我知道这是一个有点老问题但是因为我找到了一个很好的模块,我想与大家分享。

Module named Puppeteer that can make you run the Chrome in Headless mode and also interact with it via API. 名为Puppeteer的模块可以让您在无头模式下运行Chrome,并通过API与其进行交互。 So now you can create a template and take the values of the placeholders of that template via POST call and generate the PDF on the fly, in server . 现在,您可以创建一个模板,并通过POST调用获取该模板的占位符值,并在服务器中动态生成PDF

I created a small POC and here is the link to it: Demo-Puppeteer 我创建了一个小POC,这里是它的链接: Demo-Puppeteer

Let me do a bit explanation here: 我在这里做一些解释:

    ...........
    const content = await compile(templateName, data);

    /***
     * Launched the headless chrome in memory.
     */
    const browser = await puppeteer.launch();

    /***
     * Created a new page(tab)
     */
    const page = await browser.newPage();

    /***
     * Set the content of the new page
     */
    await page.setContent(content, { waitUntil: 'networkidle0' });
    /***
     * Telling chrome to emulate screen i.e how the page looks if 
     * it would have been rendered in the normal browser.
     */
    await page.emulateMedia('screen');

    const byteArray = await page.pdf({
        format: "A4",
        landscape: true,
        scale: 1.29,
        printBackground: true
    });

    const buffer = Buffer.from(byteArray, 'binary');
    /**
     * We don't need the acknowledgement of this call that is the 
     * reason we are not waiting for this call to return.
     */
    browser.close();

    return buffer;
    .......

Now, this buffer is basically a binary data and you have to write it in a file using the file module of Node. 现在,这个缓冲区基本上是一个二进制数据,你必须使用Node的文件模块将它写在一个文件中。

For further explanation please check out Exlpanation Link 如需进一步说明,请查看Exlpanation Link

You can achieve this with the help of any one of the below libraries... 您可以在以下任何一个库的帮助下实现这一目标......

FPDF - FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. FPDF - FPDF是一个PHP类,它允许使用纯PHP生成PDF文件,也​​就是说不使用PDFlib库。 F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. FPDF的F代表Free:您可以将它用于任何用途并根据您的需要进行修改。

HTML2PDF - HTML2PDF is a HTML to PDF converter written in PHP4 (use FPDF), and PHP5 (use TCPDF). HTML2PDF - HTML2PDF是一个用PHP4(使用FPDF)和PHP5(使用TCPDF)编写的HTML到PDF转换器。 It allows the conversion of valid HTML 4.01 in PDF format, and is distributed under LGPL. 它允许以PDF格式转换有效的HTML 4.01,并在LGPL下发布。

dompdf - dompdf is an HTML to PDF converter. dompdf - dompdf是一个HTML到PDF转换器。 At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP. 从本质上讲,dompdf(主要)是CSS 2.1兼容的HTML布局和用PHP编写的渲染引擎。 It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. 它是一个样式驱动的渲染器:它将下载和读取外部样式表,内联样式标记以及各个HTML元素的样式属性。 It also supports most presentational HTML attributes. 它还支持大多数表示HTML属性。

GIT location : https://github.com/dompdf/dompdf GIT位置https//github.com/dompdf/dompdf

pdfcrowd - The Pdfcrowd API is an online tool that lets you easily convert web pages and raw HTML code to PDF in your PHP applications. pdfcrowd - Pdfcrowd API是一个在线工具,可让您轻松地在PHP应用程序中将网页和原始HTML代码转换为PDF。 PDFs are generated in the cloud, no 3rd party libraries are needed. PDF是在云中生成的,不需要第三方库。 All you need is a tiny PHP API client library. 您只需要一个很小的PHP API客户端库。

You can call princexml server side like this: 您可以像这样调用princexml服务器端:

prince sample.html -o sample.pdf 

see the command-line documentation . 请参阅命令行文档

You can do this without using a browser. 您无需使用浏览器即可完成此操作。 Currently you're using jQuery to generate the HTML content, so you need the browser because jQuery runs in the browser, right? 目前你正在使用jQuery来生成HTML内容,所以你需要浏览器,因为jQuery在浏览器中运行,对吧?

You can move the generation of the HTML code to the server side by writing a server side application which generates the needed HTML content, and sends it to the pdf converter. 您可以通过编写生成所需HTML内容的服务器端应用程序将HTML代码的生成移动到服务器端,并将其发送到pdf转换器。

Princexml can be called from many server side programming languages as you can see here: http://www.princexml.com/doc/9.0/ 可以从许多服务器端编程语言中调用Princexml,如下所示: http//www.princexml.com/doc/9.0/

Choice one server side language from the list, generate the HTML in that language and send the HTML to the pdf converter. 从列表中选择一种服务器端语言,以该语言生成HTML并将HTML发送到pdf转换器。 That's all. 就这样。

Refer the documentation here http://www.princexml.com/doc/7.1/ You can choose your server side language and get the thing done. 请参阅http://www.princexml.com/doc/7.1/中的文档您可以选择服务器端语言并完成工作。 Just make a form and submit the data. 只需制作表格并提交数据即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM