Skip to content

Commit c2bf474

Browse files
committed
add converting html to pdf tutorial
1 parent 1e755d2 commit c2bf474

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
172172
- [How to Extract PDF Metadata in Python](https://www.thepythoncode.com/article/extract-pdf-metadata-in-python). ([code](handling-pdf-files/extract-pdf-metadata))
173173
- [How to Split PDF Files in Python](https://www.thepythoncode.com/article/split-pdf-files-in-python). ([code](handling-pdf-files/split-pdf))
174174
- [How to Extract Text from PDF in Python](https://www.thepythoncode.com/article/extract-text-from-pdf-in-python). ([code](handling-pdf-files/extract-text-from-pdf))
175+
- [How to Convert HTML to PDF in Python](https://www.thepythoncode.com/article/convert-html-to-pdf-in-python). ([code](handling-pdf-files/convert-html-to-pdf))
175176

176177
- ### [Python for Multimedia](https://www.thepythoncode.com/topic/python-for-multimedia)
177178
- [How to Make a Screen Recorder in Python](https://www.thepythoncode.com/article/make-screen-recorder-python). ([code](general/screen-recorder))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [How to Convert HTML to PDF in Python](https://www.thepythoncode.com/article/convert-html-to-pdf-in-python)
2+
To run this:
3+
- Install wkhtmltopdf, shown in the tutorial.
4+
- `pip3 install -r requirements.txt`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pdfkit
2+
3+
# directly from url
4+
pdfkit.from_url("https://google.com", "google.pdf", verbose=True)
5+
print("="*50)
6+
# from file
7+
pdfkit.from_file("webapp/index.html", "index.pdf", verbose=True, options={"enable-local-file-access": True})
8+
print("="*50)
9+
# from HTML content
10+
pdfkit.from_string("<p><b>Python</b> is a great programming language.</p>", "string.pdf", verbose=True)
11+
print("="*50)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pdfkit
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
3+
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
4+
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
5+
<!--[if gt IE 8]> <html class="no-js"> <!--<![endif]-->
6+
<html>
7+
<head>
8+
<meta charset="utf-8">
9+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
10+
<title></title>
11+
<meta name="description" content="">
12+
<meta name="viewport" content="width=device-width, initial-scale=1">
13+
<link rel="stylesheet" href="style.css">
14+
<style>
15+
table, th, td {
16+
border: 1px solid black;
17+
}
18+
</style>
19+
</head>
20+
<body>
21+
<!--[if lt IE 7]>
22+
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
23+
<![endif]-->
24+
<img src="image.png" alt="Python logo">
25+
<p>Sample text here. Random HTML table that is styled with CSS:</p>
26+
<table bordered>
27+
<thead>
28+
<th>ID</th>
29+
<th>Name</th>
30+
</thead>
31+
<tbody>
32+
<tr>
33+
<td>1</td>
34+
<td>Abdou</td>
35+
</tr>
36+
<tr>
37+
<td>2</td>
38+
<td>Rockikz</td>
39+
</tr>
40+
<tr>
41+
<td>3</td>
42+
<td>John</td>
43+
</tr>
44+
<tr>
45+
<td>3</td>
46+
<td>Doe</td>
47+
</tr>
48+
</tbody>
49+
</table>
50+
<p class="red-text">This should be a red paragraph.</p>
51+
</body>
52+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.red-text {
2+
color: red;
3+
}

0 commit comments

Comments
 (0)