|
| 1 | +package io.github.cloudify.scala.spdf |
| 2 | + |
| 3 | +import scala.sys.process._ |
| 4 | +import ParamShow._ |
| 5 | + |
| 6 | +/** |
| 7 | + * Holds the configuration parameters of Pdf Kit |
| 8 | + */ |
| 9 | +trait ImageConfig extends PdfConfig |
| 10 | + |
| 11 | +object ImageConfig { |
| 12 | + |
| 13 | + /** |
| 14 | + * An instance of the default configuration |
| 15 | + */ |
| 16 | + object default extends ImageConfig |
| 17 | + |
| 18 | + /** |
| 19 | + * Generates a sequence of command line parameters from a `PdfKitConfig` |
| 20 | + */ |
| 21 | + def toParameters(config: ImageConfig): Seq[String] = { |
| 22 | + import config._ |
| 23 | + Seq( |
| 24 | + allow.toParameter, |
| 25 | + background.toParameter, |
| 26 | + defaultHeader.toParameter, |
| 27 | + disableExternalLinks.toParameter, |
| 28 | + disableInternalLinks.toParameter, |
| 29 | + disableJavascript.toParameter, |
| 30 | + noPdfCompression.toParameter, |
| 31 | + disableSmartShrinking.toParameter, |
| 32 | + javascriptDelay.toParameter, |
| 33 | + enableForms.toParameter, |
| 34 | + encoding.toParameter, |
| 35 | + footerCenter.toParameter, |
| 36 | + footerFontName.toParameter, |
| 37 | + footerFontSize.toParameter, |
| 38 | + footerHtml.toParameter, |
| 39 | + footerLeft.toParameter, |
| 40 | + footerLine.toParameter, |
| 41 | + footerRight.toParameter, |
| 42 | + footerSpacing.toParameter, |
| 43 | + grayScale.toParameter, |
| 44 | + headerCenter.toParameter, |
| 45 | + headerFontName.toParameter, |
| 46 | + headerFontSize.toParameter, |
| 47 | + headerHtml.toParameter, |
| 48 | + headerLeft.toParameter, |
| 49 | + headerLine.toParameter, |
| 50 | + headerRight.toParameter, |
| 51 | + headerSpacing.toParameter, |
| 52 | + lowQuality.toParameter, |
| 53 | + marginBottom.toParameter, |
| 54 | + marginLeft.toParameter, |
| 55 | + marginRight.toParameter, |
| 56 | + marginTop.toParameter, |
| 57 | + minimumFontSize.toParameter, |
| 58 | + orientation.toParameter, |
| 59 | + outline.toParameter, |
| 60 | + outlineDepth.toParameter, |
| 61 | + pageHeight.toParameter, |
| 62 | + pageOffset.toParameter, |
| 63 | + pageSize.toParameter, |
| 64 | + pageWidth.toParameter, |
| 65 | + password.toParameter, |
| 66 | + printMediaType.toParameter, |
| 67 | + tableOfContent.toParameter, |
| 68 | + tableOfContentDepth.toParameter, |
| 69 | + tableOfContentDisableBackLinks.toParameter, |
| 70 | + tableOfContentDisableLinks.toParameter, |
| 71 | + tableOfContentFontName.toParameter, |
| 72 | + tableOfContentHeaderFontName.toParameter, |
| 73 | + tableOfContentHeaderFontSize.toParameter, |
| 74 | + tableOfContentHeaderText.toParameter, |
| 75 | + tableOfContentLevel1FontSize.toParameter, |
| 76 | + tableOfContentLevel1Indentation.toParameter, |
| 77 | + tableOfContentLevel2FontSize.toParameter, |
| 78 | + tableOfContentLevel2Indentation.toParameter, |
| 79 | + tableOfContentLevel3FontSize.toParameter, |
| 80 | + tableOfContentLevel3Indentation.toParameter, |
| 81 | + tableOfContentLevel4FontSize.toParameter, |
| 82 | + tableOfContentLevel4Indentation.toParameter, |
| 83 | + tableOfContentLevel5FontSize.toParameter, |
| 84 | + tableOfContentLevel5Indentation.toParameter, |
| 85 | + tableOfContentLevel6FontSize.toParameter, |
| 86 | + tableOfContentLevel6Indentation.toParameter, |
| 87 | + tableOfContentLevel7FontSize.toParameter, |
| 88 | + tableOfContentLevel7Indentation.toParameter, |
| 89 | + tableOfContentNoDots.toParameter, |
| 90 | + title.toParameter, |
| 91 | + userStyleSheet.toParameter, |
| 92 | + username.toParameter, |
| 93 | + useXServer.toParameter, |
| 94 | + viewportSize.toParameter, |
| 95 | + zoom.toParameter |
| 96 | + ).flatten |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Attempts to find the `wkhtmltoimage` executable in the system path. |
| 101 | + * @return |
| 102 | + */ |
| 103 | + def findExecutable: Option[String] = try { |
| 104 | + val os = System.getProperty("os.name").toLowerCase |
| 105 | + val cmd = if(os.contains("windows")) "where wkhtmltoimage" else "which wkhtmltoimage" |
| 106 | + |
| 107 | + Option(cmd.!!.trim).filter(_.nonEmpty) |
| 108 | + } catch { |
| 109 | + case _: RuntimeException => None |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments