Last active
September 25, 2019 18:28
-
-
Save leesei/1039059e4f1360a5bb781c7e2516761e to your computer and use it in GitHub Desktop.
weasyprint.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Test</title> | |
<link rel="stylesheet" href="./styles.css" /> | |
</head> | |
<body> | |
<main class="container"> | |
{% for ticket in tickets %} | |
<section class="ticket"> | |
<div class="content">{{ ticket }}</div> | |
</section> | |
{% endfor %} | |
</main> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page { | |
/* 210mm x 297mm */ | |
size: A4; | |
/* 297mm x 210mm */ | |
/* size: A4 landscape; */ | |
margin: 0; | |
} | |
body { | |
margin: 0; | |
background-color: #aaaaaa; | |
} | |
.container { | |
display: flex; | |
flex-wrap: wrap; | |
page-break-before: always; | |
} | |
/* border-box does not work for width? */ | |
.ticket { | |
box-sizing: border-box; | |
width: 91mm; /* 105 - padding * 2 */ | |
height: 59.4mm; /* 59.4 - padding * 2 - height of outline */ | |
padding: 7mm; | |
outline: 0.8px dashed black; | |
page-break-before: always; | |
} | |
.content { | |
height: 100%; | |
background-color: cyan; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 1 - Fetching and parsing HTML - HTML string | |
Step 2 - Fetching and parsing CSS - CSS string | |
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 3 - Applying CSS | |
Step 4 - Creating formatting structure | |
Step 5 - Creating layout - Page 1 | |
Step 6 - Drawing | |
Step 7 - Adding PDF metadata | |
Step 1 - Fetching and parsing HTML - HTML string | |
Step 2 - Fetching and parsing CSS - CSS string | |
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 3 - Applying CSS | |
Step 4 - Creating formatting structure | |
Step 5 - Creating layout - Page 1 | |
Step 6 - Drawing | |
Step 7 - Adding PDF metadata | |
Step 1 - Fetching and parsing HTML - HTML string | |
Step 2 - Fetching and parsing CSS - CSS string | |
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 3 - Applying CSS | |
Step 4 - Creating formatting structure | |
Step 5 - Creating layout - Page 1 | |
Step 6 - Drawing | |
Step 7 - Adding PDF metadata | |
Step 1 - Fetching and parsing HTML - HTML string | |
Step 2 - Fetching and parsing CSS - CSS string | |
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 3 - Applying CSS | |
Step 4 - Creating formatting structure | |
Step 5 - Creating layout - Page 1 | |
Step 6 - Drawing | |
Step 7 - Adding PDF metadata | |
Step 1 - Fetching and parsing HTML - HTML string | |
Step 2 - Fetching and parsing CSS - CSS string | |
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 3 - Applying CSS | |
Step 4 - Creating formatting structure | |
Step 5 - Creating layout - Page 1 | |
Step 6 - Drawing | |
Step 7 - Adding PDF metadata | |
Step 1 - Fetching and parsing HTML - HTML string | |
Step 2 - Fetching and parsing CSS - CSS string | |
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 3 - Applying CSS | |
Step 4 - Creating formatting structure | |
Step 5 - Creating layout - Page 1 | |
Step 6 - Drawing | |
Step 7 - Adding PDF metadata | |
Step 1 - Fetching and parsing HTML - HTML string | |
Step 2 - Fetching and parsing CSS - CSS string | |
Relative URI reference without a base URI: <link href="./styles.css"> | |
Step 3 - Applying CSS | |
Step 4 - Creating formatting structure | |
Step 5 - Creating layout - Page 1 | |
Step 6 - Drawing | |
Step 7 - Adding PDF metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from jinja2 import Environment, FileSystemLoader | |
from weasyprint import HTML | |
import logging | |
logger = logging.getLogger("weasyprint") | |
logger.addHandler(logging.FileHandler("./weasyprint.log")) | |
logger.setLevel("INFO") | |
env = Environment(loader=FileSystemLoader(".")) | |
template = env.get_template("pdf_template.html") | |
template_vars = {"tickets": [f"content {i}" for i in range(20)]} | |
html = template.render(template_vars) | |
HTML(string=html).write_pdf("weasyprint.pdf", stylesheets=["styles.css"]) | |
# debugging | |
with open("weasyprint.html", "w") as f: | |
f.write(html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment