-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprint_pdf.py
executable file
·225 lines (210 loc) · 8.17 KB
/
print_pdf.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
# based on: https://habr.com/ru/post/459112 (in Russian)
# https://stackoverflow.com/questions/47023842/selenium-chromedriver-printtopdf
# see also: https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/webdriver/Chromium/ChromiumDriver.cs
# https://github.com/checkly/puppeteer-examples/blob/master/1. basics/pdf.js
# NOTE: was found randomly failing with certain builds of google-chrome-stable 77
# intermittent issue of a tiny blank pdf produced (not saved)
# works fine with other vesions e.g.
# chromium-browser 79 and google-chrome-stable 76 and 72
# with google-chrome 76 the PrintToPDF API is only supported in headless mode.
# 'PrintToPDF is not implemented' exception is generated when run fullscreen
# On Windows 10 / Windows Server 2016 the altenative solution would be
# 'Microsoft Print to PDF' printer-based:
# https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/sending-powershell-results-to-pdf-part-1
# https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/sending-powershell-results-to-pdf-part-2
# https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/sending-powershell-results-to-pdf-part-3
# https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/sending-powershell-results-to-pdf-part-4
# older Windows-only solutions involve "itextsharp.dll"
# https://social.technet.microsoft.com/wiki/contents/articles/30427.creating-pdf-files-using-powershell.aspx
# and pdfsharp.dll
# https://merill.net/2013/06/creating-pdf-files-dynamically-with-powershell/
# https://github.com/empira/PDFsharp
# http://forum.oszone.net/thread-344427.html (in Russian) focused on Poowershell solitions
from __future__ import print_function
import getopt
import sys
import re
from os import getenv
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import json, base64
def print_pdf(url, chromedriver = './chromedriver', print_options = {}):
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# for annotated list of chrome headless flags, see
# https://peter.sh/experiments/chromium-command-line-switches/
# https://groups.google.com/forum/#!topic/selenium-users/SnxpvG8Erj4
flags = [
'--data-path=/tmp/data-path',
'--disable-background-timer-throttling',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-cloud-import',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-extensions',
'--disable-gesture-typing',
'--disable-gpu',
'--disable-hang-monitor',
'--disable-infobars',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-offer-upload-credit-cards',
'--disable-popup-blocking',
'--disable-print-preview',
'--disable-prompt-on-repost',
'--disable-setuid-sandbox',
'--disable-speech-api',
'--disable-sync',
'--disable-tab-for-desktop-share',
'--disable-translate',
'--disable-voice-input',
'--disable-wake-on-wifi',
'--disable-webgl',
'--disk-cache-dir=/tmp/cache-dir',
'--enable-async-dns',
'--enable-simple-cache-backend',
'--enable-tcp-fast-open',
'--headless',
'--hide-scrollbars',
'--homedir=/tmp',
'--ignore-gpu-blacklist',
'--media-cache-size=33554432',
'--metrics-recording-only',
'--mute-audio',
'--no-default-browser-check',
'--no-first-run',
'--no-pings',
'--no-sandbox',
'--no-zygote',
'--password-store=basic',
'--prerender-from-omnibox=disabled',
'--remote-debugging-port=9222',
'--single-process',
'--use-mock-keychain',
'--user-data-dir=/tmp/user-data',
'--window-size={}'.format(getenv('WINDOW_SIZE'))
]
driver = webdriver.Chrome(chromedriver, options = options)
if debug:
print('Loading url: "{}"'.format(url), file = sys.stderr)
driver.get(url)
params = {
'landscape': False,
'displayHeaderFooter': False,
'printBackground': True,
'preferCSSPageSize': False,
# Letter page sizes (the defalt)
'paperHeight': 11.0,
'paperWidth': 8.5,
'marginTop': 1.0,
'marginBottom': 0.75,
'marginLeft': 0.75,
'marginRight': 0.75,
}
params.update(print_options)
if debug:
print('params: {}'.format(params))
result = send_command_and_get_result(driver, 'Page.printToPDF', params)
# print( result.keys())
driver.quit()
return base64.b64decode(result['data'])
# https://stackoverflow.com/questions/47023842/selenium-chromedriver-printtopdf
# https://www.python-course.eu/python3_formatted_output.php
def send_command_and_get_result(driver, cmd, params = {}):
post_url = driver.command_executor._url + '/session/{0:s}/chromium/send_command_and_get_result'.format( driver.session_id)
if debug:
print ('POST to {}'.format(post_url))
print('params: {}'.format(json.dumps({'cmd': cmd, 'params': params})))
print('command: {}'.format(json.dumps({'cmd': cmd, 'params': params})))
response = driver.command_executor._request('POST', post_url, json.dumps({'cmd': cmd, 'params': params}))
if debug:
print( response.keys())
# NOTE: 'has_key()' is even removed from Python 3.x
# see also: https://stackoverflow.com/questions/1323410/should-i-use-has-key-or-in-on-python-dicts
# NOTE: KeyError: 'status'
# early implementation returns JSON with ['status', 'sessionId', 'value'] keys
# with recent versions of chrome response contains only has ['value']['data']
# print( response.keys())
if ('status' in response ) and response['status']:
raise Exception(response.get('value'))
return response.get('value')
# NOTE: on Windows 7 node occationally seeing commctl32.dll warning:
# 'A program running on this computer is trying to display a message'
# no meaningful message shown when 'View the Message' is chosen - tried multiple times
usage_str = '''usage: print_pdf.py --input <html page> --output <output file> [--size a4] [--debug]
exaple:
python3 print_pdf.py --debug --input https://www.wikipedia.org --output a.pdf --size a4
'''
if __name__ == '__main__':
# https://docs.python.org/2/library/getopt.html
try:
opts, args = getopt.getopt(sys.argv[1:], 'hdi:o:s:p:', ['help', 'debug', 'input=', 'output=','size=', 'pages='])
except getopt.GetoptError as err:
print(usage_str )
print(str(err))
exit()
output_file = None
url = None
paper_size = None
pages = None
global debug
debug = False
for option, argument in opts:
if option == '-d':
debug = True
elif option in ('-d', '--debug'):
debug = True
elif option in ('-h', '--help'):
print (usage_str)
exit()
elif option in ('-i', '--input'):
url = argument
elif option in ('-p', '--pages'):
pages = argument
elif option in ('-s', '--size'):
paper_size = argument
elif option in ('-o', '--output'):
output_file = argument
else:
assert False, 'unhandled option: {}'.format(option)
if url == None or output_file == None:
print (usage_str)
exit()
if getenv('OS') != None :
homedir = getenv('USERPROFILE').replace('\\', '/')
else:
homedir = getenv('HOME')
# NOTE: when schema prefix is omitted from the url, an exception is raised:
# selenium.common.exceptions.InvalidArgumentException:
# Message: invalid argument (Session info: headless chrome=76.0.3809.100)
match = re.match(r'^(https?://).*$', url, re.UNICODE)
if match == None:
url = 'https://{}'.format(url)
match = None
if paper_size != None:
match = re.match(r'a4', paper_size, re.UNICODE|re.IGNORECASE )
if match == None:
print_options = {}
else:
# specify custom page size (default is Letter), through dimensions
print_options = {
'paperWidth': 8.27,
'paperHeight': 11.69,
'marginTop': 1.0,
'marginBottom': 1.44,
'marginLeft': 0.75,
'marginRight': 0.52,
}
if pages != None:
print_options.update({'pageRanges': pages})
if debug:
print('opts: {}'.format(opts))
# exit()
result = print_pdf(url, homedir + '/' + 'Downloads' + '/' + 'chromedriver', print_options)
with open(file = output_file, mode = 'wb') as f:
f.write(result)
# on vanilla Windows node
# PATH=%PATH%;c:\Python27;%USERPROFILE%\Downloads