4

A couple of questions go in this direction on StackOverflow, but are off-topic there, so let's have an on-topic question here.

It would be nice if you put an example screenshot in your answers for each tool / library you suggest.

Also, please note:

  • CSS: Does the tool / library apply CSS?
    • @page: Does it apply landscape / portrait mode?
    • Fonts? (This question is only for tools/libraries that respect @font-face)
    • Colors?
  • JavaScript: Does it apply JavaScript before the PDF is generated?
  • or not
  • or not
  • What are the requirements?

Example 1

You could use the following as an example for testing:

<!DOCTYPE html>
<html>
<head>
    <title>HTML 2 PDF print test</title>

    <style type="text/css">
        body {
            font-size: 14px;
            color:  #333;
        }

        table {
            width: 100%;
            max-width: 100%;
            border-spacing: 0;
            border-collapse: collapse;
            font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
        }

        th {
            text-align: left;
        }

        td, th {
            vertical-align: top;
            border-top: 1px solid #ddd;
            line-height: 1.42857143;
            padding: 8px;
        }

        tbody tr:nth-child(odd) {
           background-color: #f9f9f9 !important;
        }

        @media print{
            @page {size: landscape}
        }
    </style>
</head>
<body>
<table class="table table-striped">
    <tr>
        <th style="font-family:Courier New;">Country</th>
        <th style="color: red;">Code</th>
        <th style="color: red !important;">Phone</th>
        <th>Language</th>
        <th>Population</th>
        <th>Banana Rama</th>
        <th>Foo bar</th>
        <th>Constants</th>
        <th>Empty Cells</th>
        <th>More</th>
        <th>End</th>
    </tr>
    <tr>
        <td>Germany</td>
        <td>de</td>
        <td>+49</td>
        <td>German</td>
        <td>82 Million</td>
        <td id="cell">JavaScript does <span style="color: red">not</span> work.</td>
        <td>dasfd asfawerf asdfvas fwer </td>
        <td>asd fasdf asdfa sdfa sdf asdf asdf asdfa sdf asd</td>
        <td>-</td>
        <td>asdf asdfasd fasdf asdfa sdfasdf</td>
        <td>ad fasd fasd fasd fasdf asd fasdf as</td>
    </tr>
    <tr>
        <td>France</td>
        <td>fr</td>
        <td>+01</td>
        <td>French</td>
        <td>70 Million</td>
        <td>a &nbsp;asdfa sdf asdf asdf asdfasd asdf asdf asd fasd fasdfa sdf</td>
        <td>aerte fasf werwasdfa sd3e asdf adfasdfe werfa sdfas</td>
        <td>as dfasd fasd fasd fasd fasd&nbsp;</td>
        <td></td>
        <td>asd fasd fasdf asdf asd fasdf asdf asd fasdf asdf&nbsp;</td>
        <td>as dfa sd asfdas asfd&nbsp;</td>
    </tr>
    <tr>
        <td>Great Britain</td>
        <td>uk</td>
        <td>+02</td>
        <td>English</td>
        <td>60 Million</td>
        <td>asdfasdf asdf asdfa sdf asdfasdf&nbsp;</td>
        <td>asd fasdf asdf asdfwr wadfa sd f</td>
        <td>ada asdf asd</td>
        <td></td>
        <td>a sd fasd fasdf asdf asdfa sdf a</td>
        <td>asasd asdf asd fasd fas asd fasdf&nbsp;</td>
    </tr>
    <tr>
        <td>United States of America</td>
        <td>us</td>
        <td>+03</td>
        <td>English</td>
        <td>300 Million</td>
        <td>asdf asdfasd fasdfwerwfasdfasdf asdfasdfasd&nbsp;</td>
        <td>a dfasdf asdf asdf rt asdf asdfasd asdf asd fasd fas</td>
        <td>a dsfas fasd f</td>
        <td></td>
        <td>a dsfa sdfasd fasd f</td>
        <td>a dsfasd asdf asdf asd fafasd fas fas&nbsp;</td>
    </tr>
</table>
<script type="text/javascript">
    var cell = document.getElementById("cell");
    cell.innerHTML = "JavaScript <span style='color: green; font-weight:bold'>works</span>";
</script>
</body>
</html>

Chrome makes the following out of it with its print function:

enter image description here

Things to note:

  • The page is in landscape mode ✓
  • The font is changed for Country ✓
  • Code is red ✓
  • Phone is red ✓
  • The table is striped ✓
  • JavaScript is applied ✓
  • What I've seen so far: pdfkit, pandoc, PrinceXML, weasyprint, wkhtmltopdf. – Martin Thoma Sep 21 '17 at 15:08
  • puppeteer was recommended to me. – Martin Thoma Sep 22 '17 at 7:26
3

Sejda PDF

Sejda PDF is a commercial software package to process PDF files, including conversion of HTML to PDF.

Javascript, CSS3, custom fonts are all supported.

The HTML to PDF feature is currently in beta (feedback welcome!)

Installation

No installation is required.

Sejda's HTML to PDF feature is provided as an online service that works in the browser. https://www.sejda.com/html-to-pdf

The online service can be used for free for up to 3 conversions per hour.

A REST API for HTML to PDF conversion is also available.

Usage

Convert HTML to PDF in your browser using Sejda PDF

Example 1

enter image description here

  • The page is in landscape mode ✓
  • The font is changed for Country ✓
  • Code is red ✓
  • Phone is red ✓
  • The table is striped ✓
  • JavaScript works ✓
  • The font is not changed for country. Look at the y. If it was changed, the y would have an underbar. In your case, it hasn't. – Martin Thoma Sep 27 '17 at 9:26
  • Hi Martin, I think the font did change, but instead of Courier New it was using an alternative font. Just updated the screenshot with the latest output, now Courier New is used. – Edi Sep 27 '17 at 13:16
  • Interesting. So I guess you are the developer of that page? – Martin Thoma Sep 27 '17 at 14:31
  • 1
    I work at Sejda and am involved with the development of this feature. – Edi Sep 28 '17 at 8:06
2

weasyprint

weasyprint is a and Python package which comes with an executable. Documentation is online, code is on Github. Last commit was on 19.09.2017. It has 1484 stars, 155 forks and 41 contributors.

Installation

$ pip install weasyprint

Usage

$ weasyprint input.html output.pdf

Example 1

enter image description here

Please note:

  • The page is in landscape mode ✓
  • The font is changed for Country ✓
  • Code is red ✓
  • Phone is red ✓
  • The table is striped ✓
  • JavaScript does not work ✘
1

PrinceXML

PrinceXML is a commercial software package. It can be installed in various ways, including a Debian package. A user documentation is available.

It has a free version which adds an icon to the generated PDF. The Server license costs US$3800.

Installation

See the installation guide

After installing PrinceXML, you should have prince in your PATH:

$ prince --version
Prince 11.3
Copyright 2002-2017 YesLogic Pty. Ltd.
Non-commercial License

Usage

$ prince input.html -o output.pdf

Example 1

This looks very similar to Weasyprint. In fact, I can't see any difference

enter image description here

  • The page is in landscape mode ✓
  • The font is changed for Country ✓
  • Code is red ✓
  • Phone is red ✓
  • The table is not striped ✓
  • JavaScript is not applied ✘
  • DocRaptor is a paid API tool that uses the Prince library in the background (but has a much stronger JavaScript engine). Besides the JS and larger support team, DocRaptor offers a much lower entry price than Prince's $3k. – jamespaden Sep 22 '17 at 20:04
  • Additionally, Prince does support JavaScript (though its support is lagging modern browsers), you just have to enable it. It's off by default to speed up document parsing. – jamespaden Sep 22 '17 at 20:05
1

wkhtmltopdf

wkhtmltopdf is a and (LGPLv3, source) command line to convert HTML files to PDF. It has 5794 stars, 860 forks and 53 contributors on GitHub. It is written in C++. The first commit on GitHub was in May 7, 2008.

Installation

$ sudo apt-get install wkhtmltopdf

Usage

$ wkhtmltopdf input.html output.pdf

Example 1

enter image description here

  • The page is not in landscape mode ✘ (see feature request), but it can be set with -O landscape
  • The font is changed for Country ✓
  • Code is red ✓
  • Phone is red ✓
  • The table is striped ✓
  • JavaScript works ✓
  • Better documentation would be good, but otherwise it works as advertised and is just the tool I need. – Tim Mar 26 at 14:01
0

Yuo can use python script: https://github.com/labadze/html2pdf-pyhton

There is full description and usage. Hope helps you.

0

Get a print driver that does not print to a printer, it prints to a PDF. Adobe Distiller comes with one. There are also 3rd party PDF printers too.

  1. View web page in browser.
  2. Click Print, then choose PDF printer.
  3. PDF printer will ask you where you want to save the file. Enter a path and filename.
  4. Done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.