Html to pdf conversion at server side?
Asked Answered
F

7

5

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 ?

Fraya answered 10/1, 2015 at 10:10 Comment(4)
Sure - just don't do it on the client, but on the server.Melissamelisse
Instead of sending the genereated HTML page to the client, you send it to the converter instead.Captivate
hello meskobalazs,i dont understand what you said,can you please explain it clearly..Fraya
tcpdf.org ?Tobit
E
6

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

Edyth answered 29/1, 2019 at 14:50 Comment(1)
sample command: google-chrome --headless --print-to-pdf=out.pdf file.html. and a question: any idea whether it is possible to avoid rebooting chrome every time you create a pdf? thanks!Spurt
L
3

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

Laspisa answered 12/1, 2015 at 10:46 Comment(0)
L
3

Check out wkhtmltopdf - Convert HTML to PDF using Webkit (QtWebKit) @ http://wkhtmltopdf.org

Lashelllasher answered 18/1, 2015 at 3:10 Comment(1)
This is the only free solution that I found that does a descend job on HTML5 based content.Kelleykelli
I
2

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.

Indiscriminate answered 12/1, 2015 at 11:33 Comment(0)
G
1

You can call princexml server side like this:

prince sample.html -o sample.pdf 

see the command-line documentation.

Greenery answered 12/1, 2015 at 12:22 Comment(0)
B
1

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.

Ballarat answered 19/1, 2015 at 9:49 Comment(0)
O
0

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.

Oxendine answered 16/1, 2015 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.