简体   繁体   中英

Html to pdf conversion at server side?

we are creating a pdf file from the html file using the package princexml pdf converter. For the Creation of the html file the data has given by the server. In the browser using jquery the input string(html code) is created for the pdf creation. 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.

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/ . 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

This allows you to use jquery and everything else - since it uses the acutal rendering engine. I guess you do not even need princexml then. There is also a faq page regarding capture: 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. 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 .

I created a small POC and here is the link to it: 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.

For further explanation please check out 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. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.

HTML2PDF - HTML2PDF is a HTML to PDF converter written in PHP4 (use FPDF), and PHP5 (use TCPDF). It allows the conversion of valid HTML 4.01 in PDF format, and is distributed under LGPL.

dompdf - dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in 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. It also supports most presentational HTML attributes.

GIT location : 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. PDFs are generated in the cloud, no 3rd party libraries are needed. All you need is a tiny PHP API client library.

You can call princexml server side like this:

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?

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.

Princexml can be called from many server side programming languages as you can see here: 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. 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. Just make a form and submit the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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