options.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package wkhtmltopdf
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. //A list of options that can be set from code to make it easier to see which options are available
  7. type globalOptions struct {
  8. CookieJar stringOption //Read and write cookies from and to the supplied cookie jar file
  9. Copies uintOption //Number of copies to print into the pdf file (default 1)
  10. Dpi uintOption //Change the dpi explicitly (this has no effect on X11 based systems)
  11. ExtendedHelp boolOption //Display more extensive help, detailing less common command switches
  12. Grayscale boolOption //PDF will be generated in grayscale
  13. Help boolOption //Display help
  14. HTMLDoc boolOption //Output program html help
  15. ImageDpi uintOption //When embedding images scale them down to this dpi (default 600)
  16. ImageQuality uintOption //When jpeg compressing images use this quality (default 94)
  17. License boolOption //Output license information and exit
  18. Lowquality boolOption //Generates lower quality pdf/ps. Useful to shrink the result document space
  19. ManPage boolOption //Output program man page
  20. MarginBottom uintOption //Set the page bottom margin
  21. MarginLeft uintOption //Set the page left margin (default 10mm)
  22. MarginRight uintOption //Set the page right margin (default 10mm)
  23. MarginTop uintOption //Set the page top margin
  24. Orientation stringOption // Set orientation to Landscape or Portrait (default Portrait)
  25. NoCollate boolOption //Do not collate when printing multiple copies (default collate)
  26. PageHeight uintOption //Page height
  27. PageSize stringOption //Set paper size to: A4, Letter, etc. (default A4)
  28. PageWidth uintOption //Page width
  29. NoPdfCompression boolOption //Do not use lossless compression on pdf objects
  30. Quiet boolOption //Be less verbose
  31. ReadArgsFromStdin boolOption //Read command line arguments from stdin
  32. Readme boolOption //Output program readme
  33. Title stringOption //The title of the generated pdf file (The title of the first document is used if not specified)
  34. Version boolOption //Output version information and exit
  35. }
  36. func (gopt *globalOptions) Args() []string {
  37. return optsToArgs(gopt)
  38. }
  39. type outlineOptions struct {
  40. DumpDefaultTocXsl boolOption //Dump the default TOC xsl style sheet to stdout
  41. DumpOutline stringOption //Dump the outline to a file
  42. NoOutline boolOption //Do not put an outline into the pdf
  43. OutlineDepth uintOption //Set the depth of the outline (default 4)
  44. }
  45. func (oopt *outlineOptions) Args() []string {
  46. return optsToArgs(oopt)
  47. }
  48. type pageOptions struct {
  49. Allow sliceOption //Allow the file or files from the specified folder to be loaded (repeatable)
  50. NoBackground boolOption //Do not print background
  51. CacheDir stringOption //Web cache directory
  52. CheckboxCheckedSvg stringOption //Use this SVG file when rendering checked checkboxes
  53. CheckboxSvg stringOption //Use this SVG file when rendering unchecked checkboxes
  54. Cookie mapOption //Set an additional cookie (repeatable), value should be url encoded
  55. CustomHeader mapOption //Set an additional HTTP header (repeatable)
  56. CustomHeaderPropagation boolOption //Add HTTP headers specified by --custom-header for each resource request
  57. NoCustomHeaderPropagation boolOption //Do not add HTTP headers specified by --custom-header for each resource request
  58. DebugJavascript boolOption //Show javascript debugging output
  59. DefaultHeader boolOption //Add a default header, with the name of the page to the left, and the page number to the right, this is short for: --header-left='[webpage]' --header-right='[page]/[toPage]' --top 2cm --header-line
  60. Encoding stringOption //Set the default text encoding, for input
  61. DisableExternalLinks boolOption //Do not make links to remote web pages
  62. EnableForms boolOption //Turn HTML form fields into pdf form fields
  63. NoImages boolOption //Do not load or print images
  64. DisableInternalLinks boolOption //Do not make local links
  65. DisableJavascript boolOption //Do not allow web pages to run javascript
  66. JavascriptDelay uintOption //Wait some milliseconds for javascript finish (default 200)
  67. LoadErrorHandling stringOption //Specify how to handle pages that fail to load: abort, ignore or skip (default abort)
  68. LoadMediaErrorHandling stringOption //Specify how to handle media files that fail to load: abort, ignore or skip (default ignore)
  69. DisableLocalFileAccess boolOption //Do not allowed conversion of a local file to read in other local files, unless explicitly allowed with --allow
  70. MinimumFontSize uintOption //Minimum font size
  71. ExcludeFromOutline boolOption //Do not include the page in the table of contents and outlines
  72. PageOffset uintOption //Set the starting page number (default 0)
  73. Password stringOption //HTTP Authentication password
  74. EnablePlugins boolOption //Enable installed plugins (plugins will likely not work)
  75. Post mapOption //Add an additional post field (repeatable)
  76. PostFile mapOption //Post an additional file (repeatable)
  77. PrintMediaType boolOption //Use print media-type instead of screen
  78. Proxy stringOption //Use a proxy
  79. RadiobuttonCheckedSvg stringOption //Use this SVG file when rendering checked radiobuttons
  80. RadiobuttonSvg stringOption //Use this SVG file when rendering unchecked radiobuttons
  81. RunScript sliceOption //Run this additional javascript after the page is done loading (repeatable)
  82. DisableSmartShrinking boolOption //Disable the intelligent shrinking strategy used by WebKit that makes the pixel/dpi ratio none constant
  83. NoStopSlowScripts boolOption //Do not Stop slow running javascripts
  84. EnableTocBackLinks boolOption //Link from section header to toc
  85. UserStyleSheet stringOption //Specify a user style sheet, to load with every page
  86. Username stringOption //HTTP Authentication username
  87. ViewportSize stringOption //Set viewport size if you have custom scrollbars or css attribute overflow to emulate window size
  88. WindowStatus stringOption //Wait until window.status is equal to this string before rendering page
  89. Zoom floatOption //Use this zoom factor (default 1)
  90. }
  91. func (popt *pageOptions) Args() []string {
  92. return optsToArgs(popt)
  93. }
  94. type headerAndFooterOptions struct {
  95. FooterCenter stringOption //Centered footer text
  96. FooterFontName stringOption //Set footer font name (default Arial)
  97. FooterFontSize uintOption //Set footer font size (default 12)
  98. FooterHTML stringOption //Adds a html footer
  99. FooterLeft stringOption //Left aligned footer text
  100. FooterLine boolOption //Display line above the footer
  101. FooterRight stringOption //Right aligned footer text
  102. FooterSpacing floatOption //Spacing between footer and content in mm (default 0)
  103. HeaderCenter stringOption //Centered header text
  104. HeaderFontName stringOption //Set header font name (default Arial)
  105. HeaderFontSize uintOption //Set header font size (default 12)
  106. HeaderHTML stringOption //Adds a html header
  107. HeaderLeft stringOption //Left aligned header text
  108. HeaderLine boolOption //Display line below the header
  109. HeaderRight stringOption //Right aligned header text
  110. HeaderSpacing floatOption //Spacing between header and content in mm (default 0)
  111. Replace mapOption //Replace [name] with value in header and footer (repeatable)
  112. }
  113. func (hopt *headerAndFooterOptions) Args() []string {
  114. return optsToArgs(hopt)
  115. }
  116. type tocOptions struct {
  117. DisableDottedLines boolOption //Do not use dotted lines in the toc
  118. TocHeaderText stringOption //The header text of the toc (default Table of Contents)
  119. TocLevelIndentation uintOption //For each level of headings in the toc indent by this length (default 1em)
  120. DisableTocLinks boolOption //Do not link from toc to sections
  121. TocTextSizeShrink floatOption //For each level of headings in the toc the font is scaled by this factor
  122. XslStyleSheet stringOption //Use the supplied xsl style sheet for printing the table of content
  123. }
  124. func (topt *tocOptions) Args() []string {
  125. return optsToArgs(topt)
  126. }
  127. type argParser interface {
  128. Parse() []string //Used in the cmd call
  129. }
  130. type stringOption struct {
  131. option string
  132. value string
  133. }
  134. func (so stringOption) Parse() []string {
  135. args := []string{}
  136. if so.value == "" {
  137. return args
  138. }
  139. args = append(args, "--"+so.option)
  140. args = append(args, so.value)
  141. return args
  142. }
  143. func (so *stringOption) Set(value string) {
  144. so.value = value
  145. }
  146. type sliceOption struct {
  147. option string
  148. value []string
  149. }
  150. func (so sliceOption) Parse() []string {
  151. args := []string{}
  152. if len(so.value) == 0 {
  153. return args
  154. }
  155. for _, v := range so.value {
  156. args = append(args, "--"+so.option)
  157. args = append(args, v)
  158. }
  159. return args
  160. }
  161. func (so *sliceOption) Set(value string) {
  162. so.value = append(so.value, value)
  163. }
  164. type mapOption struct {
  165. option string
  166. value map[string]string
  167. }
  168. func (mo mapOption) Parse() []string {
  169. args := []string{}
  170. if mo.value == nil || len(mo.value) == 0 {
  171. return args
  172. }
  173. for k, v := range mo.value {
  174. args = append(args, "--"+mo.option)
  175. args = append(args, k)
  176. args = append(args, v)
  177. }
  178. return args
  179. }
  180. func (mo *mapOption) Set(key, value string) {
  181. if mo.value == nil {
  182. mo.value = make(map[string]string)
  183. }
  184. mo.value[key] = value
  185. }
  186. type uintOption struct {
  187. option string
  188. value uint
  189. isSet bool
  190. }
  191. func (io uintOption) Parse() []string {
  192. args := []string{}
  193. if io.isSet == false {
  194. return args
  195. }
  196. args = append(args, "--"+io.option)
  197. args = append(args, fmt.Sprintf("%d", io.value))
  198. return args
  199. }
  200. func (io *uintOption) Set(value uint) {
  201. io.isSet = true
  202. io.value = value
  203. }
  204. type floatOption struct {
  205. option string
  206. value float64
  207. isSet bool
  208. }
  209. func (fo floatOption) Parse() []string {
  210. args := []string{}
  211. if fo.isSet == false {
  212. return args
  213. }
  214. args = append(args, "--"+fo.option)
  215. args = append(args, fmt.Sprintf("%.3f", fo.value))
  216. return args
  217. }
  218. func (fo *floatOption) Set(value float64) {
  219. fo.isSet = true
  220. fo.value = value
  221. }
  222. type boolOption struct {
  223. option string
  224. value bool
  225. }
  226. func (bo boolOption) Parse() []string {
  227. if bo.value {
  228. return []string{"--" + bo.option}
  229. }
  230. return []string{}
  231. }
  232. func (bo *boolOption) Set(value bool) {
  233. bo.value = value
  234. }
  235. func newGlobalOptions() globalOptions {
  236. return globalOptions{
  237. CookieJar: stringOption{option: "cookie-jar"},
  238. Copies: uintOption{option: "copies"},
  239. Dpi: uintOption{option: "dpi"},
  240. ExtendedHelp: boolOption{option: "extended-help"},
  241. Grayscale: boolOption{option: "grayscale"},
  242. Help: boolOption{option: "true"},
  243. HTMLDoc: boolOption{option: "htmldoc"},
  244. ImageDpi: uintOption{option: "image-dpi"},
  245. ImageQuality: uintOption{option: "image-quality"},
  246. License: boolOption{option: "license"},
  247. Lowquality: boolOption{option: "lowquality"},
  248. ManPage: boolOption{option: "manpage"},
  249. MarginBottom: uintOption{option: "margin-bottom"},
  250. MarginLeft: uintOption{option: "margin-left"},
  251. MarginRight: uintOption{option: "margin-right"},
  252. MarginTop: uintOption{option: "margin-top"},
  253. Orientation: stringOption{option: "orientation"},
  254. NoCollate: boolOption{option: "nocollate"},
  255. PageHeight: uintOption{option: "page-height"},
  256. PageSize: stringOption{option: "page-size"},
  257. PageWidth: uintOption{option: "page-width"},
  258. NoPdfCompression: boolOption{option: "no-pdf-compression"},
  259. Quiet: boolOption{option: "quiet"},
  260. ReadArgsFromStdin: boolOption{option: "read-args-from-stdin"},
  261. Readme: boolOption{option: "readme"},
  262. Title: stringOption{option: "title"},
  263. Version: boolOption{option: "version"},
  264. }
  265. }
  266. func newOutlineOptions() outlineOptions {
  267. return outlineOptions{
  268. DumpDefaultTocXsl: boolOption{option: "dump-default-toc-xsl"},
  269. DumpOutline: stringOption{option: "dump-outline"},
  270. NoOutline: boolOption{option: "no-outline"},
  271. OutlineDepth: uintOption{option: "outline-depth"},
  272. }
  273. }
  274. func newPageOptions() pageOptions {
  275. return pageOptions{
  276. Allow: sliceOption{option: "allow"},
  277. NoBackground: boolOption{option: "no-background"},
  278. CacheDir: stringOption{option: "cache-dir"},
  279. CheckboxCheckedSvg: stringOption{option: "checkbox-checked-svg"},
  280. CheckboxSvg: stringOption{option: "checkbox-svg"},
  281. Cookie: mapOption{option: "cookie"},
  282. CustomHeader: mapOption{option: "custom-header"},
  283. CustomHeaderPropagation: boolOption{option: "custom-header-propagation"},
  284. NoCustomHeaderPropagation: boolOption{option: "no-custom-header-propagation"},
  285. DebugJavascript: boolOption{option: "debug-javascript"},
  286. DefaultHeader: boolOption{option: "default-header"},
  287. Encoding: stringOption{option: "encoding"},
  288. DisableExternalLinks: boolOption{option: "disable-external-links"},
  289. EnableForms: boolOption{option: "enable-forms"},
  290. NoImages: boolOption{option: "no-images"},
  291. DisableInternalLinks: boolOption{option: "disable-internal-links"},
  292. DisableJavascript: boolOption{option: "disable-javascript "},
  293. JavascriptDelay: uintOption{option: "javascript-delay"},
  294. LoadErrorHandling: stringOption{option: "load-error-handling"},
  295. LoadMediaErrorHandling: stringOption{option: "load-media-error-handling"},
  296. DisableLocalFileAccess: boolOption{option: "disable-local-file-access"},
  297. MinimumFontSize: uintOption{option: "minimum-font-size"},
  298. ExcludeFromOutline: boolOption{option: "exclude-from-outline"},
  299. PageOffset: uintOption{option: "page-offset"},
  300. Password: stringOption{option: "password"},
  301. EnablePlugins: boolOption{option: "enable-plugins"},
  302. Post: mapOption{option: "post"},
  303. PostFile: mapOption{option: "post-file"},
  304. PrintMediaType: boolOption{option: "print-media-type"},
  305. Proxy: stringOption{option: "proxy"},
  306. RadiobuttonCheckedSvg: stringOption{option: "radiobutton-checked-svg"},
  307. RadiobuttonSvg: stringOption{option: "radiobutton-svg"},
  308. RunScript: sliceOption{option: "run-script"},
  309. DisableSmartShrinking: boolOption{option: "disable-smart-shrinking"},
  310. NoStopSlowScripts: boolOption{option: "no-stop-slow-scripts"},
  311. EnableTocBackLinks: boolOption{option: "enable-toc-back-links"},
  312. UserStyleSheet: stringOption{option: "user-style-sheet"},
  313. Username: stringOption{option: "username"},
  314. ViewportSize: stringOption{option: "viewport-size"},
  315. WindowStatus: stringOption{option: "window-status"},
  316. Zoom: floatOption{option: "zoom"},
  317. }
  318. }
  319. func newHeaderAndFooterOptions() headerAndFooterOptions {
  320. return headerAndFooterOptions{
  321. FooterCenter: stringOption{option: "footer-center"},
  322. FooterFontName: stringOption{option: "footer-font-name"},
  323. FooterFontSize: uintOption{option: "footer-font-size"},
  324. FooterHTML: stringOption{option: "footer-html"},
  325. FooterLeft: stringOption{option: "footer-left"},
  326. FooterLine: boolOption{option: "footer-line"},
  327. FooterRight: stringOption{option: "footer-right"},
  328. FooterSpacing: floatOption{option: "footer-spacing"},
  329. HeaderCenter: stringOption{option: "header-center"},
  330. HeaderFontName: stringOption{option: "header-font-name"},
  331. HeaderFontSize: uintOption{option: "header-font-size"},
  332. HeaderHTML: stringOption{option: "header-html"},
  333. HeaderLeft: stringOption{option: "header-left"},
  334. HeaderLine: boolOption{option: "header-line"},
  335. HeaderRight: stringOption{option: "header-right"},
  336. HeaderSpacing: floatOption{option: "header-spacing"},
  337. Replace: mapOption{option: "replace"},
  338. }
  339. }
  340. func newTocOptions() tocOptions {
  341. return tocOptions{
  342. DisableDottedLines: boolOption{option: "disable-dotted-lines"},
  343. TocHeaderText: stringOption{option: "toc-header-text"},
  344. TocLevelIndentation: uintOption{option: "toc-level-indentation"},
  345. DisableTocLinks: boolOption{option: "disable-toc-links"},
  346. TocTextSizeShrink: floatOption{option: "toc-text-size-shrink"},
  347. XslStyleSheet: stringOption{option: "xsl-style-sheet"},
  348. }
  349. }
  350. func optsToArgs(opts interface{}) []string {
  351. args := []string{}
  352. rv := reflect.Indirect(reflect.ValueOf(opts))
  353. if rv.Kind() != reflect.Struct {
  354. return args
  355. }
  356. for i := 0; i < rv.NumField(); i++ {
  357. prsr, ok := rv.Field(i).Interface().(argParser)
  358. if ok {
  359. s := prsr.Parse()
  360. if len(s) > 0 {
  361. args = append(args, s...)
  362. }
  363. }
  364. }
  365. return args
  366. }
  367. // Constants for orientation modes
  368. const (
  369. OrientationLandscape = "Landscape" // Landscape mode
  370. OrientationPortrait = "Portrait" // Portrait mode
  371. )
  372. // Constants for page sizes
  373. const (
  374. PageSizeA0 = "A0" // 841 x 1189 mm
  375. PageSizeA1 = "A1" // 594 x 841 mm
  376. PageSizeA2 = "A2" // 420 x 594 mm
  377. PageSizeA3 = "A3" // 297 x 420 mm
  378. PageSizeA4 = "A4" // 210 x 297 mm, 8.26
  379. PageSizeA5 = "A5" // 148 x 210 mm
  380. PageSizeA6 = "A6" // 105 x 148 mm
  381. PageSizeA7 = "A7" // 74 x 105 mm
  382. PageSizeA8 = "A8" // 52 x 74 mm
  383. PageSizeA9 = "A9" // 37 x 52 mm
  384. PageSizeB0 = "B0" // 1000 x 1414 mm
  385. PageSizeB1 = "B1" // 707 x 1000 mm
  386. PageSizeB2 = "B2" // 500 x 707 mm
  387. PageSizeB3 = "B3" // 353 x 500 mm
  388. PageSizeB4 = "B4" // 250 x 353 mm
  389. PageSizeB5 = "B5" // 176 x 250 mm, 6.93
  390. PageSizeB6 = "B6" // 125 x 176 mm
  391. PageSizeB7 = "B7" // 88 x 125 mm
  392. PageSizeB8 = "B8" // 62 x 88 mm
  393. PageSizeB9 = "B9" // 33 x 62 mm
  394. PageSizeB10 = "B10" // 31 x 44 mm
  395. PageSizeC5E = "C5E" // 163 x 229 mm
  396. PageSizeComm10E = "Comm10E" // 105 x 241 mm, U.S. Common 10 Envelope
  397. PageSizeDLE = "DLE" // 110 x 220 mm
  398. PageSizeExecutive = "Executive" // 7.5 x 10 inches, 190.5 x 254 mm
  399. PageSizeFolio = "Folio" // 210 x 330 mm
  400. PageSizeLedger = "Ledger" // 431.8 x 279.4 mm
  401. PageSizeLegal = "Legal" // 8.5 x 14 inches, 215.9 x 355.6 mm
  402. PageSizeLetter = "Letter" // 8.5 x 11 inches, 215.9 x 279.4 mm
  403. PageSizeTabloid = "Tabloid" // 279.4 x 431.8 mm
  404. PageSizeCustom = "Custom" // Unknown, or a user defined size.
  405. )