Skip to content

PDF WeasyPrint Cave


How to run these scripts

Install WeasyPrint


Sample RPA Test Page

The scripts reference a custom RPA Test Page

The installation for Windows was problematic (ie there were problems) so the examples here and videos show WeasyPrint running on Ubuntu and using Python .

The code is not too difficult it may be the install that is the most complicated.


Video: Running WeasyPrint Code Samples

The video shows the code samples running as Python files and the PDF output produced.


Script 1: Create a PDF from a Web Page

Script 01
from weasyprint import HTML

# capture url to PDF

HTML('https://verdapress.uk/services/test-bed/').write_pdf('vpWebPage.pdf')

Script 2: Create a PDF from a Local File

Script 02
from weasyprint import HTML

# capture local HTML to PDF

html = HTML(filename ='data.html')
html.write_pdf(
    'local.pdf'
)

data.html for local file
<html>
<body>

<!-- Local HTML file input to PDF -->

<article> A List of Headings to generate Bookmarks</article>

<h1>Link h1</h1>
<h2>Link h2</h2>
<h3>Link h3</h3>
<h4>Link h4</h4>
<h5>Link h5</h5>
<h6>Link h6</h6>

</body>
</html>

Script 3: Create a PDF Add Bookmarks

Script 03
from weasyprint import HTML

# capture bookmarks = H1 -> h6

html = HTML(filename ='data.html')
html.write_pdf(
    'bookmarks.pdf'
)

Script 4: Create a PDF include attachment

Script 04
from weasyprint import HTML

# attach file to PDF

html = HTML(filename ='data.html')
html.write_pdf(
    'attachment.pdf', version = '1.3', attachments = ['vpWebPage.pdf']
)

Script 5: Create a PDF from String

Script 05
from weasyprint import HTML

# capture $tring to PDF

html = HTML(string='<h1>The title of the Document</h1><p>Paragraph text goes here</p>')
html.write_pdf(
    'stringobject.pdf') 

Script 6: Create a PDF include CSS

Script 06
from weasyprint import HTML, CSS

# add CSS to change text to red or use css file

HTML('https://verdapress.uk/services/test-bed/').write_pdf('css_page.pdf',
stylesheets=[CSS(string='body {color:red !important;}')])
# stylesheets=[CSS(filename ='style.css')])

Hint

Download project scripts