I recently published an . In it, I explored options like the native functionality, open-source libraries and , and the paid service . Here's a quick recap of my findings: article comparing HTML-to-PDF export libraries browser print jsPDF pdfmake DocRaptor If you want the simplest solution and don’t need a professional-looking document, the native browser print functionality should be just fine. If you need more control over the PDF output, then you’ll want to use a library. jsPDF shines when it comes to single-page content generated based on HTML shown in the UI. pdfmake works best when generating PDF content from data rather than from HTML. DocRaptor is the most powerful of them all with its simple API and its beautiful PDF output. But again, unlike the others, it is a paid service. However, if your business depends on elegant, professional document generation, DocRaptor is well worth the cost. In the comment section for my article on Dev.to, I take a look at and as additional alternatives to consider. (This person is Andreas Zettl by the way, and he has an awesome demo site full of .) one person suggested Paged.js WeasyPrint Print CSS examples So today we'll explore the relative strengths and weaknesses of DocRaptor and WeasyPrint. WeasyPrint Overview Let's start with WeasyPrint, an open-source library developed by and supported by . For starters, , which is a plus. It's licensed under the , a relatively permissive and straightforward license. WeasyPrint allows you to generate content as either a PDF or a PNG, which should adequately cover most use cases. It's built for Python 3.6+, which is great if you're a Python developer. If Python is not your forte or not part of your company's tech stack, then this may be a non-starter for you. Kozea Court Bouillon it's free BSD 3-Clause License One of the biggest caveats to be aware of is that ! So when using this library, you'll need to be exporting content that is generated server-side. If you are relying on dynamically generated content or charts and tables powered by JavaScript, this library is not for you. WeasyPrint does not support JavaScript-generated content Installing WeasyPrint Getting up and running with WeasyPrint is fairly easy. They provide on their website, but I use to rather than Homebrew, so my installation steps looked more like this: installation instructions pyenv install and manage Python Installing and Python: pyenv brew install pyenv pyenv install 3.7.3 pyenv global 3.7.3 pyenv version -e >> ~/.zshrc # install pyenv using Homebrew # install Python 3.7.3 using pyenv # specify that I'd like to use version 3.7.3 when I use Python # quick sanity check # add `pyenv init` to my shell to enable shims and autocompletion echo 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' Installing WeasyPrint and running it against the WeasyPrint website: pip install WeasyPrint weasyprint https://weasyprint.org/ weasyprint.pdf As you can see, the simplest way to use WeasyPrint from your terminal is to run the command with two arguments: the URL input and the filename output. This creates a file called in the directory from which you run the command. Here's a screenshot of the PDF output when viewed in the Preview app on a Mac: weasyprint weasyprint.pdf Sample PDF output from WeasyPrint Looks great! WeasyPrint also has a full you can check out which showcases , , and even complete with a barcode. page of examples reports invoices event tickets DocRaptor Overview Now let's consider DocRaptor. is closed-source and is available through a paid license subscription (although you can generate test documents for free). It uses the PrinceXML HTML-to-PDF engine and is the only API powered by this technology. DocRaptor Unlike WeasyPrint's Python-only usage, DocRaptor has SDKs for PHP, Python, Node, Ruby, Java, .NET, and JavaScript/jQuery. It can also be used directly via an HTTP request, so you can generate a PDF right from your terminal using cURL. This is great news if you're someone like me who doesn't have Python in their arsenal. DocRaptor can export content as a PDF, XLS, or XLSX document. This can come in handy if your content is meant to be a table compatible with Excel. For the time being though, we'll just look at PDFs since that's something both WeasyPrint and DocRaptor support. One relative strength of DocRaptor compared to WeasyPrint is that it wait for JavaScript on the page to be executed, so it's perfect for use with dynamically generated content and charting libraries. can Getting Started with DocRaptor DocRaptor has that are well worth reading when first trying out their service. Since we ran the WeasyPrint example from the command line, let's also by using cURL to make an HTTP request. DocRaptor is API-based, so there's no need to download or install anything. guides for each of their SDKs run DocRaptor in our terminal Here's their example you can try: curl http://YOUR_API_KEY_HERE@docraptor.com/docs \ --fail --silent --show-error \ --header \ --data > docraptor.pdf "Content-Type:application/json" '{"test": true, "document_url": "http://docraptor.com/examples/invoice.html", "type": "pdf" }' And here's the output after running that code snippet in your terminal: Sample PDF output from DocRaptor Voila: a nice and simple invoice. DocRaptor's example here isn't as complex as WeasyPrint's was, so let's try generating a PDF from one of DocRaptor's more advanced examples. curl http://YOUR_API_KEY_HERE@docraptor.com/docs \ --fail --silent --show-error \ --header \ --data > docraptor_cookbook.pdf "Content-Type:application/json" '{"test": true, "document_url": "https://docraptor.com/samples/cookbook.html", "type": "pdf" }' Here's the output for this cookbook recipe PDF: Sample PDF output from DocRaptor using their Cookbook Recipe example Pretty neat! Just like WeasyPrint, DocRaptor can handle complex designs and full-bleed layouts that extend to the very edge of the page. One important callout here is that DocRaptor supports footnotes, as seen in this example. WeasyPrint, on the other hand, has not yet fully implemented the , so it can't handle footnote generation. CSS paged media specifications You can view more DocRaptor including a , a , an , and an . examples on their site financial statement brochure invoice e-book JavaScript Execution So far we've seen the powers and similarities of both DocRaptor and WeasyPrint. But one core difference we touched on above is that WeasyPrint does not wait for JavaScript to execute before generating the PDF. This is crucial for applications built with a framework like React. By default, React apps contain only a root container in the HTML, and then JavaScript runs to inject the React components onto the page. div So if you try to generate a PDF from the command line for an app built with React, you won't get the actual app content! Instead, you'll likely see the content of the tag, which typically contains a message stating something like "You need to enable JavaScript to run this app." noscript This is also the case for applications that rely on charting libraries like , , or . Without the JavaScript running, no chart is created. Google Charts HighCharts Chart.js As an example, I've put together. It contains a page header, a paragraph included in the HTML source code, and a paragraph inserted into the DOM by JavaScript. You can find the . Here's what the page looks like: consider this simple web page code on GitHub DocRaptor JS demo web page Now, let's use WeasyPrint to generate a PDF from the web page by running the following command in the terminal: weasyprint http://tylerhawkins.info/docraptor-js-demo/ weasyprint_js_demo.pdf Here's the output: JS demo PDF output from WeasyPrint Oh no! Where's the second paragraph? It's not there, because the JavaScript was never executed. Now let's try again, but this time with DocRaptor. In order to have JavaScript run on the page, we must provide DocRaptor with the argument in our options object. Here's the code: "javascript": true curl http://YOUR_API_KEY_HERE@docraptor.com/docs \ --fail --silent --show-error \ --header \ --data > docraptor_js_demo.pdf "Content-Type:application/json" '{"test": true, "javascript": true, "document_url": "http://tylerhawkins.info/docraptor-js-demo/", "type": "pdf" }' And the output: JS demo PDF output from DocRaptor Tada! The JavaScript has been successfully executed, leading to the insertion of the second paragraph. Conclusion So, which should you use, WeasyPrint or DocRaptor? It depends on your use case. If your app contains static content that doesn't rely on JavaScript, if Python is part of your tech stack, or if you need PNG image output, then WeasyPrint is an excellent choice. It's open source, free, and flexible enough to handle visually complex output. If you need to use a programming language other than Python, or you rely on the execution of JavaScript to render the content you need exported, DocRaptor is the right choice. Table of Comparisons As an added bonus, here's a comparison table for a quick summary of these two libraries: DocRaptor vs. WeasyPrint comparison table Happy coding! Also published at https://dzone.com/articles/docraptor-vs-weasyprint-a-pdf-export-showdown