-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathsample_convert_html_to_pdf.py
More file actions
69 lines (56 loc) · 2.15 KB
/
sample_convert_html_to_pdf.py
File metadata and controls
69 lines (56 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# coding: utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: sample_convert_html_to_pdf.py
DESCRIPTION:
This sample demonstrates how to convert a html file to the pdf file.
PREREQUISITES:
Before using this function, need to install following required component:
1).Install python-pdfkit:
"$ pip install pdfkit"
2).Install wkhtmltopdf:
-Debian/Ubuntu:
"$ sudo apt-get install wkhtmltopdf"
-macOS:
"$ brew install homebrew/cask/wkhtmltopdf"
-Windows and other options: check https://wkhtmltopdf.org/downloads.html for wkhtmltopdf binary installers
More information about pdfkit, reference from https://pypi.org/project/pdfkit/.
USAGE:
python sample_convert_html_to_pdf.py
"""
import os
import pdfkit
def convert_html_file_to_pdf(html_path, save_pdf_path):
with open(html_path, "r", encoding="utf-8") as f:
htmlStr = f.read()
directory_to_save_pdf = os.path.dirname(save_pdf_path)
if not os.path.isdir(directory_to_save_pdf):
os.makedirs(directory_to_save_pdf)
return pdfkit.from_string(htmlStr, save_pdf_path)
if __name__ == "__main__":
current_file_path = os.path.abspath(__file__)
current_folder_path = os.path.dirname(current_file_path)
path_of_sample_html = os.path.abspath(
os.path.join(
current_file_path,
"..",
"..",
"..",
"./Data/other-doc-type/web-page.html",
)
)
path_to_save_pdf = os.path.abspath(
os.path.join(
current_folder_path,
"./result/converted.pdf",
)
)
isSuccessful = convert_html_file_to_pdf(path_of_sample_html, path_to_save_pdf)
if isSuccessful:
print(f"Convert pdf successfully in {path_to_save_pdf}")
else:
print("Something wrong. Check the html file please.")