From b3bb5dc4f466cb6579788d2aaa2cc13e411a7ce1 Mon Sep 17 00:00:00 2001 From: chungonn Date: Mon, 30 Jul 2018 14:47:46 +0800 Subject: [PATCH] Auto detect xvfb-run and will use it together with wkhtmltopdf if exists --- project/build.properties | 2 +- .../io/github/cloudify/scala.spdf/Pdf.scala | 21 ++++++++++++------- .../cloudify/scala.spdf/PdfConfig.scala | 6 ++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/project/build.properties b/project/build.properties index 27e88aa..d6e3507 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.13.13 +sbt.version=1.1.6 diff --git a/src/main/scala/io/github/cloudify/scala.spdf/Pdf.scala b/src/main/scala/io/github/cloudify/scala.spdf/Pdf.scala index dce595f..149fdd6 100644 --- a/src/main/scala/io/github/cloudify/scala.spdf/Pdf.scala +++ b/src/main/scala/io/github/cloudify/scala.spdf/Pdf.scala @@ -22,14 +22,19 @@ class Pdf(executablePath: String, config: PdfConfig) { /** * Generates the command line needed to execute `wkhtmltopdf` */ - private def toCommandLine[A: SourceDocumentLike, B: DestinationDocumentLike](source: A, destination: B): Seq[String] = - Seq(executablePath) ++ - PdfConfig.toParameters(config) ++ - Seq( - "--quiet", - implicitly[SourceDocumentLike[A]].commandParameter(source), - implicitly[DestinationDocumentLike[B]].commandParameter(destination) - ) + private def toCommandLine[A: SourceDocumentLike, B: DestinationDocumentLike](source: A, destination: B): Seq[String] = { + val XVFB = "xvfb-run" + val execPath = PdfConfig.findExecutablePath(XVFB) match { + case Some(xvfbPath) if xvfbPath.contains(XVFB) => Seq(xvfbPath, "--", executablePath) + case _ => Seq(executablePath) + } + + execPath ++ PdfConfig.toParameters(config) ++ Seq( + "--quiet", + implicitly[SourceDocumentLike[A]].commandParameter(source), + implicitly[DestinationDocumentLike[B]].commandParameter(destination) + ) + } /** * Check whether the executable is actually executable, if it isn't diff --git a/src/main/scala/io/github/cloudify/scala.spdf/PdfConfig.scala b/src/main/scala/io/github/cloudify/scala.spdf/PdfConfig.scala index 3912655..697c8e5 100644 --- a/src/main/scala/io/github/cloudify/scala.spdf/PdfConfig.scala +++ b/src/main/scala/io/github/cloudify/scala.spdf/PdfConfig.scala @@ -253,9 +253,11 @@ object PdfConfig { * Attempts to find the `wkhtmltopdf` executable in the system path. * @return */ - def findExecutable: Option[String] = try { + def findExecutable: Option[String] = findExecutablePath("wkhtmltopdf") + + def findExecutablePath(exec: String): Option[String] = try { val os = System.getProperty("os.name").toLowerCase - val cmd = if(os.contains("windows")) "where wkhtmltopdf" else "which wkhtmltopdf" + val cmd = if(os.contains("windows")) s"where $exec" else s"which $exec" Option(cmd.!!.trim).filter(_.nonEmpty) } catch {