- Client Side Setup
- jsPDF
- Example
- Server Side Setup
- wkhtmltopdf
- Python (pdfkit module)
- Pros and Cons
- jsPDF and html2canvas
- wkhtmltopdf
Import the latest version to your page using a CDN.
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.min.js"></script>
This Javascript library allows you to easily create PDF files and make it downloadable to the user.
As an example of this:
var doc = new jsPDF();
doc.text('Hello world!', 10, 10);
doc.save('a4.pdf');
Further examples can be found on their documentation page.
We can find the following example here.
We will write a function that in this example will be called when the website content has been loaded.
- We create a canvas using the library html2canvas, which we must download first. Keep in mind that you must use version 1.0.0 for this.
- We then retrieve the jpeg image data from it.
- Now we create a new jsPDF object with the parameters:
p
,mm
anda4
.- With
p
we say the pdf should be in portrait mode. - After that, we say with
mm
that we want to use millimeters to calculate with. - We tell jsPDF with
a4
that we want a DIN-A4 sized page.
- With
- After this, we get the image and page height and width.
- Then we calculate the ratio. We then use a ternary operator to determine if the ratio of imageWidth/imageHeight is bigger than the pageWidth/pageHeight. And if that’s the case we set the
ratio
variable to the pageWidth/imageWidth, otherwise we set it to the pageHeight/imageHeight. - Now we use the
addImage()
function from our new jsPDF object. With it, we insert the image data. And insert the position. - In the end, we make the user download the pdf file by calling the
save()
function.
Here is the corresponding code to this example:
function print() {
html2canvas(document.querySelector('body')).then(canvas => {
var imgData = canvas.toDataURL('image/jpeg');
var pdf = new jsPDF('p', 'mm', 'a4');
var pageWidth = pdf.internal.pageSize.width;
var pageHeight = pdf.internal.pageSize.height;
var imageWidth = canvas.width;
var imageHeight = canvas.height;
var ratio = imageWidth/imageHeight >= pageWidth/pageHeight ? pageWidth/imageWidth : pageHeight/imageHeight;
pdf.addImage(imgData, 'JPEG', 0, 0, imageWidth * ratio, imageHeight * ratio);
pdf.save('demo.pdf');
});
}
wkhtmltopdf is an extensive command-line tool to render HTML to PDF, while using the QT WebKit Engine.
To set up, wkhtmltopdf download the binaries from here. Choose a Stable version and your OS.
For Ubuntu/Debian just enter sudo apt install wkhtmltopdf
into your command line.
After the install, add the /bin/
folder of wkhtmltopdf to your PATH.
Now, reload all your command line instances and you can now use it properly.
As an example, you can convert a webpage to a pdf file by entering: wkhtmltopdf https://wikipedia.org/wiki/Wiki demo.pdf
. This will create the demo.pdf file in your current folder.
If you want to use this for Python-specific, there’s the pdfkit module, which you can install with the pip packet manager. pip install pdfkit
. But keep in mind that you must install wkhtmltopdf as well to use it.
Here’s a quick example of how you can convert a webpage to HTML with pdfkit:
import pdfkit
# Create a PDF file from a URL.
pdfkit.from_url('https://en.wikipedia.org/wiki/Wiki', 'demo1.pdf')
# Create a PDF file from a file.
pdfkit.from_file('../client_side/wiki.html', 'demo2.pdf')
You have the option to convert a pdf file from a webpage or a local HTML file.
We can find this example in particular here.
- Pros
- Uses Client resources to convert the HTML file.
- Very flexible and stores any CSS content as it makes a screenshot of the section.
- Cons
- Text and SVG files are not scaleable.
- The text is not highlightable.
- Content can get blurry.
- It doesn’t support multiple pages.
- Extremely high file size.
- Pros
- The text is highlightable.
- Text and SVG files are scaleable.
- Handles multiple pages very well.
- Small file size.
- Cons
- Encoding errors can happen.
- Uses server resources.