-
Notifications
You must be signed in to change notification settings - Fork 122
/
pdfpy.py
60 lines (41 loc) · 1.42 KB
/
pdfpy.py
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
import pdfkit
import os
from PyPDF2 import PdfFileMerger
from PyPDF2.utils import PdfReadError
class PdfEngine(object):
"""
This class carries operations on pdf files.
It has the following methods:
convert() --- Which converts each of the markup file
passed in to pdf. Markup file should be html
combine() --- Which merges all of the pdf files created by
the convert method, creating a new file.
del_pdf() --- Which deletes all the pdf files created by
the convert method.
"""
def __init__(self, markup_files, style_files, pdf_files, directory):
self.markup_files = markup_files
self.style_files = style_files
self.pdf_files = pdf_files
self.directory = directory
def convert(self):
for each in self.markup_files:
# Prevent conversion process from showing terminal updates
options = {"enable-local-file-access": None, "quiet": ""}
pdfkit.from_file(each, "{}.pdf".format(self.markup_files.index(each)),
options=options)
print('--- Sections converted to pdf')
def combine(self):
merger = PdfFileMerger()
for pdf in self.pdf_files:
try:
merger.append(pdf, import_bookmarks=False)
except PdfReadError:
pass
merger.write("{}.pdf".format(self.directory))
print('--- Sections combined together in a single pdf file')
merger.close()
def del_pdf(self):
for each in self.pdf_files:
os.remove(each)
print('--- Individual pdf files deleted from directory')