-
Notifications
You must be signed in to change notification settings - Fork 0
/
pjhtmltoimage.coffee
executable file
·137 lines (123 loc) · 7.34 KB
/
pjhtmltoimage.coffee
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
#!/usr/bin/env phantomjs
#
# PhantomJS drop-in replacement for wkhtmltoimage
#
# Copyright (c) 2013 Aaron Stone <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
helpBanner = '''
PhantomJS drop-in for wkhtmltopdf and wkhtmltoimage by Aaron Stone
Usage: pjhtmltoimage [options] URL filename
'''
switches = [
['-h', '--help', 'Display help']
['-H', '--extended-help', 'Display more extensive help, detailing less common command switches']
['-V', '--version', 'Output version information an exit']
[ '--manpage', 'Output program man page']
[ '--readme', 'Output program readme']
['-f', '--format [FORMAT]', 'Output file format (default is jpg)']
['-n', '--disable-javascript', 'Do not allow web pages to run javascript']
['-p', '--proxy [PROXY]', 'Use a proxy']
['-0', '--disable-smart-width*', 'Use the specified width even if it is not large enough for the content']
[ '--allow [PATH]', 'Allow the file or files from the specified folder to be loaded (repeatable)']
[ '--checkbox-checked-svg [PATH]', 'Use this SVG file when rendering checked checkboxes']
[ '--checkbox-svg [PATH]', 'Use this SVG file when rendering unchecked checkboxes']
[ '--cookie [NAME] [VALUE]', 'Set an additional cookie (repeatable)']
[ '--cookie-jar [PATH]', 'Read and write cookies from and to the supplied cookie jar file']
[ '--crop-h [INT]', 'Set height for croping']
[ '--crop-w [INT]', 'Set width for croping']
[ '--crop-x [INT]', 'Set x coordinate for croping']
[ '--crop-y [INT]', 'Set y coordinate for croping']
[ '--custom-header [NAME] [VALUE]', 'Set an additional HTTP header (repeatable)']
[ '--custom-header-propagation', 'Add HTTP headers specified by --custom-header for each resource request.']
[ '--no-custom-header-propagation', 'Do not add HTTP headers specified by --custom-header for each resource request.']
[ '--debug-javascript', 'Show javascript debugging output']
[ '--no-debug-javascript', 'Do not show javascript debugging output (default)']
[ '--encoding [ENCODING]', 'Set the default text encoding, for input']
[ '--height [HEIGHT]', 'Set screen height (default is calculated from page content) (default 0)']
[ '--htmldoc', 'Output program html help']
[ '--images', 'Do load or print images (default)']
[ '--no-images', 'Do not load or print images']
[ '--enable-javascript', 'Do allow web pages to run javascript (default)']
[ '--javascript-delay [MSEC]', 'Wait some milliseconds for javascript finish (default 200)']
[ '--load-error-handling [HANDER]', 'Specify how to handle pages that fail to load: abort, ignore or skip (default abort)']
[ '--disable-local-file-access', 'Do not allowed conversion of a local file to read in other local files, unless explecitily allowed with --allow']
[ '--enable-local-file-access', 'Allowed conversion of a local file to read in other local files. (default)']
[ '--minimum-font-size [INT]', 'Minimum font size']
[ '--password [PASSWORD]', 'HTTP Authentication password']
[ '--disable-plugins', 'Disable installed plugins (default)']
[ '--enable-plugins', 'Enable installed plugins (plugins will likely not work)']
[ '--post [NAME] [VALUE]', 'Add an additional post field (repeatable)']
[ '--post-file [VALUE] [PATH]', 'Post an additional file (repeatable)']
[ '--quality [INT]', 'Output image quality (between 0 and 100) (default 94)']
[ '--radiobutton-checked-svg [PATH]', 'Use this SVG file when rendering checked radiobuttons']
[ '--radiobutton-svg [PATH]', 'Use this SVG file when rendering unchecked radiobuttons']
[ '--run-script [JS]', 'Run this additional javascript after the page is done loading (repeatable)']
[ '--stop-slow-scripts', 'Stop slow running javascripts (default)']
[ '--no-stop-slow-scripts', 'Do not Stop slow running javascripts (default)']
[ '--transparent*', 'Make the background transparent in pngs']
[ '--user-style-sheet [URL]', 'Specify a user style sheet, to load with every page']
[ '--username [USERNAME]', 'HTTP Authentication username']
[ '--width [INT]', 'Set screen width (default is 1024) (default 1024)']
[ '--window-status [STATUS]', 'Wait until window.status is equal to this string before rendering page']
[ '--zoom [FLOAT]', 'Use this zoom factor (default 1)']
]
optparse = require './lib/optparse'
parser = new optparse.OptionParser switches, helpBanner
fs = require 'fs'
system = require 'system'
options = parser.parse system.args.slice 1
# Arguments
address = options.arguments[0]
output = options.arguments[1]
output = '/dev/stdout' if output is '-'
# Defaults
options['format'] ||= 'jpg'
options['width'] ||= 600
options['height'] ||= 600
options['quality'] ||= 75
options['javascript-delay'] ||= 200
options['crop-h'] ||= options['height']
options['crop-w'] ||= options['width']
options['crop-x'] ||= 0
options['crop-y'] ||= 0
if options.help or system.args.length is 1
console.log parser.help()
phantom.exit 1
page = require('webpage').create()
page.viewportSize = {
width: options['width'],
height: options['height']
}
page.clipRect = {
top: options['crop-y'],
left: options['crop-x'],
width: options['crop-w'],
height: options['crop-h']
}
# page.paperSize = { width: size[0], height: size[1], border: '0px' }
# page.paperSize = { format: system.args[3], orientation: 'portrait', border: '1cm' }
if address is '-'
page.content = fs.read('/dev/stdin')
else
page.open address, (status) ->
if status isnt 'success'
console.log 'Unable to load the address!'
phantom.exit()
window.setTimeout (-> (page.render output, { format: options['format'], quality: options['quality'] }; phantom.exit())), options['javascript-delay']