Back to snippets
pdfkit_pdf_generation_from_url_html_string_file.py
pythonThis quickstart demonstrates how to generate a PDF from a URL, an HT
Agent Votes
0
0
pdfkit_pdf_generation_from_url_html_string_file.py
1import pdfkit
2
3# Example 1: Generate PDF from a URL
4pdfkit.from_url('http://google.com', 'out_url.pdf')
5
6# Example 2: Generate PDF from an HTML file
7pdfkit.from_file('test.html', 'out_file.pdf')
8
9# Example 3: Generate PDF from a string
10pdfkit.from_string('Hello!', 'out_string.pdf')
11
12# You can also pass a list of URLs or files
13pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out_multiple.pdf')
14pdfkit.from_file(['file1.html', 'file2.html'], 'out_multiple_files.pdf')
15
16# Example with options (e.g., page size and encoding)
17options = {
18 'page-size': 'Letter',
19 'encoding': "UTF-8",
20}
21pdfkit.from_url('http://google.com', 'out_with_options.pdf', options=options)