From c42c633b57e9a66a33f63fecd67a679bc46eb2ed Mon Sep 17 00:00:00 2001 From: Herdy Handoko Date: Sat, 17 Aug 2019 09:45:49 +0800 Subject: [PATCH 1/6] Update Scala to `2.12.9` --- examples/play26-example/build.sbt | 4 ++-- examples/play27-example/build.sbt | 4 ++-- modules/play26/build.sbt | 4 ++-- modules/play27/build.sbt | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/play26-example/build.sbt b/examples/play26-example/build.sbt index ea7c4d2..e2eedda 100644 --- a/examples/play26-example/build.sbt +++ b/examples/play26-example/build.sbt @@ -28,9 +28,9 @@ name := """play26-scala-pdf-example""" version := "1.0.0" -scalaVersion := "2.12.8" +scalaVersion := "2.12.9" -crossScalaVersions := Seq("2.11.12", "2.12.8") +crossScalaVersions := Seq("2.11.12", "2.12.9") libraryDependencies ++= Seq( guice, diff --git a/examples/play27-example/build.sbt b/examples/play27-example/build.sbt index 34888d1..7137ea9 100644 --- a/examples/play27-example/build.sbt +++ b/examples/play27-example/build.sbt @@ -28,9 +28,9 @@ name := """play27-scala-pdf-example""" version := "1.0.0" -scalaVersion := "2.12.8" +scalaVersion := "2.12.9" -crossScalaVersions := Seq("2.11.12", "2.12.8") +crossScalaVersions := Seq("2.11.12", "2.12.9") libraryDependencies ++= Seq( guice, diff --git a/modules/play26/build.sbt b/modules/play26/build.sbt index 828c827..2f7f061 100644 --- a/modules/play26/build.sbt +++ b/modules/play26/build.sbt @@ -32,9 +32,9 @@ organization := "com.hhandoko" version := Source.fromFile("../../VERSION.txt").mkString.trim -scalaVersion := "2.12.8" +scalaVersion := "2.12.9" -crossScalaVersions := Seq("2.11.12", "2.12.8") +crossScalaVersions := Seq("2.11.12", "2.12.9") libraryDependencies ++= Seq( guice, diff --git a/modules/play27/build.sbt b/modules/play27/build.sbt index 01ffa4a..f6265f3 100644 --- a/modules/play27/build.sbt +++ b/modules/play27/build.sbt @@ -32,9 +32,9 @@ organization := "com.hhandoko" version := Source.fromFile("../../VERSION.txt").mkString.trim -scalaVersion := "2.12.8" +scalaVersion := "2.12.9" -crossScalaVersions := Seq("2.11.12", "2.12.8") +crossScalaVersions := Seq("2.11.12", "2.12.9") libraryDependencies ++= Seq( guice, From b9fa4d8186a99342368d760e9444d464724fc164 Mon Sep 17 00:00:00 2001 From: Herdy Handoko Date: Sat, 17 Aug 2019 10:08:43 +0800 Subject: [PATCH 2/6] Update to latest Play 2.6 and 2.7 version --- examples/play26-example/project/plugins.sbt | 2 +- examples/play27-example/project/plugins.sbt | 2 +- modules/play26/project/plugins.sbt | 2 +- modules/play27/project/plugins.sbt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/play26-example/project/plugins.sbt b/examples/play26-example/project/plugins.sbt index 5fbd11b..b562c59 100644 --- a/examples/play26-example/project/plugins.sbt +++ b/examples/play26-example/project/plugins.sbt @@ -1,2 +1,2 @@ // Play Framework plugin -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.21") +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.23") diff --git a/examples/play27-example/project/plugins.sbt b/examples/play27-example/project/plugins.sbt index 03e02dd..614a139 100644 --- a/examples/play27-example/project/plugins.sbt +++ b/examples/play27-example/project/plugins.sbt @@ -1,2 +1,2 @@ // Play Framework plugin -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0") +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3") diff --git a/modules/play26/project/plugins.sbt b/modules/play26/project/plugins.sbt index 1be1600..7a37acb 100644 --- a/modules/play26/project/plugins.sbt +++ b/modules/play26/project/plugins.sbt @@ -1,5 +1,5 @@ // Play Framework plugin -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.21") +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.23") // Maven publishing-specific // ~~~~~ diff --git a/modules/play27/project/plugins.sbt b/modules/play27/project/plugins.sbt index f29f773..6b58343 100644 --- a/modules/play27/project/plugins.sbt +++ b/modules/play27/project/plugins.sbt @@ -1,5 +1,5 @@ // Play Framework plugin -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0") +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3") // Maven publishing-specific // ~~~~~ From cf9b9fbd56eafd9ead01e2c4f4e3c3528786ae2c Mon Sep 17 00:00:00 2001 From: Herdy Handoko Date: Sat, 17 Aug 2019 10:48:17 +0800 Subject: [PATCH 3/6] Add Scala 2.13 support for Play 2.7 module --- examples/play27-example/build.sbt | 4 +-- .../com/hhandoko/play/pdf/PdfGenerator.scala | 25 ++++++++++++++----- .../com/hhandoko/play/pdf/PdfUserAgent.scala | 2 +- modules/play27/build.sbt | 6 ++--- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/examples/play27-example/build.sbt b/examples/play27-example/build.sbt index 7137ea9..644b523 100644 --- a/examples/play27-example/build.sbt +++ b/examples/play27-example/build.sbt @@ -28,9 +28,9 @@ name := """play27-scala-pdf-example""" version := "1.0.0" -scalaVersion := "2.12.9" +scalaVersion := "2.13.0" -crossScalaVersions := Seq("2.11.12", "2.12.9") +crossScalaVersions := Seq("2.11.12", "2.12.9", "2.13.0") libraryDependencies ++= Seq( guice, diff --git a/modules/play27/app/com/hhandoko/play/pdf/PdfGenerator.scala b/modules/play27/app/com/hhandoko/play/pdf/PdfGenerator.scala index 8e019c0..88f6582 100644 --- a/modules/play27/app/com/hhandoko/play/pdf/PdfGenerator.scala +++ b/modules/play27/app/com/hhandoko/play/pdf/PdfGenerator.scala @@ -118,8 +118,9 @@ class PdfGenerator(env: Environment, val xhtml: Boolean = false) { * @param fonts the external / additional fonts to load. * @return Generated PDF result (with "application/pdf" MIME type). */ - def ok(html: Html, documentBaseUrl: String, fonts: Seq[String] = defaultFonts): Result = { - Results.Ok(toBytes(parseString(html), documentBaseUrl, fonts)).as("application/pdf") + def ok(html: Html, documentBaseUrl: String, fonts: Seq[String] = Nil): Result = { + val loadedFonts = defaultIfEmpty(fonts) + Results.Ok(toBytes(parseString(html), documentBaseUrl, loadedFonts)).as("application/pdf") } /** @@ -133,7 +134,7 @@ class PdfGenerator(env: Environment, val xhtml: Boolean = false) { def toBytes(html: Html, documentBaseUrl: String, fonts: Seq[String]): Array[Byte] = { // NOTE: Use default value assignment in method body, // as Scala compiler does not like overloaded methods with default params - val loadedFonts = if (fonts.isEmpty) defaultFonts else fonts + val loadedFonts = defaultIfEmpty(fonts) toBytes(parseString(html), documentBaseUrl, loadedFonts) } @@ -148,7 +149,7 @@ class PdfGenerator(env: Environment, val xhtml: Boolean = false) { def toBytes(string: String, documentBaseUrl: String, fonts: Seq[String]): Array[Byte] = { // NOTE: Use default value assignment in method body, // as Scala compiler does not like overloaded methods with default params - val loadedFonts = if (fonts.isEmpty) defaultFonts else fonts + val loadedFonts = defaultIfEmpty(fonts) val output = new ByteArrayOutputStream() toStream(output)(string, documentBaseUrl, loadedFonts) output.toByteArray @@ -162,10 +163,11 @@ class PdfGenerator(env: Environment, val xhtml: Boolean = false) { * @param documentBaseUrl the document / page base URL. * @param fonts the external / additional fonts to load. */ - def toStream(output: OutputStream)(string: String, documentBaseUrl: String, fonts: Seq[String] = defaultFonts): Unit = { + def toStream(output: OutputStream)(string: String, documentBaseUrl: String, fonts: Seq[String] = Nil): Unit = { + val loadedFonts = defaultIfEmpty(fonts) val input = new ByteArrayInputStream(string.getBytes("UTF-8")) val renderer = new ITextRenderer() - fonts.foreach { font => renderer.getFontResolver.addFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED) } + loadedFonts.foreach { font => renderer.getFontResolver.addFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED) } val userAgent = new PdfUserAgent(env, renderer.getOutputDevice) userAgent.setSharedContext(renderer.getSharedContext) renderer.getSharedContext.setUserAgentCallback(userAgent) @@ -190,4 +192,15 @@ class PdfGenerator(env: Environment, val xhtml: Boolean = false) { } else html.body } + /** + * Use default fonts if given fonts list is empty. + * + * @note `defaultFonts.toSeq` is used as a universal conversion method due to `Seq` predef changes from mutable to + * immutable collections. + * @param fonts the fonts to load. + * @return Fonts to load or default fonts as fallback. + */ + private[this] def defaultIfEmpty(fonts: Seq[String]): Seq[String] = + if (fonts.isEmpty) defaultFonts.toSeq else fonts + } diff --git a/modules/play27/app/com/hhandoko/play/pdf/PdfUserAgent.scala b/modules/play27/app/com/hhandoko/play/pdf/PdfUserAgent.scala index 40a1725..b72465d 100644 --- a/modules/play27/app/com/hhandoko/play/pdf/PdfUserAgent.scala +++ b/modules/play27/app/com/hhandoko/play/pdf/PdfUserAgent.scala @@ -114,7 +114,7 @@ class PdfUserAgent(env: Environment, outputDevice: ITextOutputDevice) extends IT */ private def toByteArray(stream: InputStream): Array[Byte] = { val buffer = new BufferedInputStream(stream) - Stream.continually(buffer.read).takeWhile(-1 !=).map(_.toByte).toArray + Stream.continually(buffer.read).takeWhile(-1 != _).map(_.toByte).toArray } /** diff --git a/modules/play27/build.sbt b/modules/play27/build.sbt index f6265f3..e01c563 100644 --- a/modules/play27/build.sbt +++ b/modules/play27/build.sbt @@ -32,9 +32,9 @@ organization := "com.hhandoko" version := Source.fromFile("../../VERSION.txt").mkString.trim -scalaVersion := "2.12.9" +scalaVersion := "2.13.0" -crossScalaVersions := Seq("2.11.12", "2.12.9") +crossScalaVersions := Seq("2.11.12", "2.12.9", "2.13.0") libraryDependencies ++= Seq( guice, @@ -55,7 +55,7 @@ libraryDependencies ++= Seq( // ScalaTest + Play plugin // - http://www.scalatest.org/plus/play - "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.1" % Test + "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.3" % Test ) resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases" From 128f810af8356b62c0b031e1b96c392723f77c2f Mon Sep 17 00:00:00 2001 From: Herdy Handoko Date: Sat, 17 Aug 2019 10:50:59 +0800 Subject: [PATCH 4/6] Update README with Scala 2.13 support information --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e5d80a0..6a39f42 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,12 @@ It simply renders Play Framework HTML and CSS-based view templates to PDF via [F The supported Scala and Play versions as follows: -| | Scala 2.10 | Scala 2.11 | Scala 2.12 | -| --------- |:----------:|:----------:|:----------:| -| Play 2.4 | `YES` | `YES` | | -| Play 2.5 | | `YES` | | -| Play 2.6 | | `YES` | `YES` | -| Play 2.7 | | `YES` | `YES` | +| | Scala 2.10 | Scala 2.11 | Scala 2.12 | Scala 2.13 | +| --------- |:----------:|:----------:|:----------:|:----------:| +| Play 2.4 | `YES` | `YES` | | | +| Play 2.5 | | `YES` | | | +| Play 2.6 | | `YES` | `YES` | | +| Play 2.7 | | `YES` | `YES` | `YES` | ### Play Framework Java @@ -35,7 +35,7 @@ def providePdfGenerator(): PdfGenerator = { pdfGen.loadLocalFonts(Seq("fonts/opensans-regular.ttf")) pdfGen } -``` +``` Currently, the module is hosted at Maven Central Repository. Include the following lines in ```build.sbt```, then reload SBT to resolve and enable the module: ``` scala @@ -52,7 +52,7 @@ resolvers ++= Seq( ) ``` -*NOTE: If you are using an IDE like Eclipse, remember to re-generate your project files.* +*NOTE: If you are using an IDE like Eclipse, remember to re-generate your project files.* ## Usage @@ -70,9 +70,9 @@ You can use a standard Play Framework Scala template like this one: Then this template, after having imported ```com.hhandoko.play.pdf.PdfGenerator```, can simply be rendered as: ``` scala class HomeController @Inject() (pdfGen: PdfGenerator) extends Controller { - + def examplePdf = Action { pdfGen.ok(views.html.example(), "http://localhost:9000") } - + } ``` @@ -89,7 +89,7 @@ Please observe the following constraints to avoid issues when using this module: - Non-system fonts must be loaded explicitly (see below for example) - External assets such as images need to be loaded as base64-encoded string - External such as stylesheets will be loaded as per normal HTML page load - + *NOTE: If the specified URI is a path into Play Framework app classpath, the resource is loaded directly instead. See the above sample template for an example.* Fonts can be loaded by invoking `PdfGenerator.loadLocalFonts` method. For example: From c11ca41516afc2b60a3e4b4a779817b3e1653a5a Mon Sep 17 00:00:00 2001 From: Herdy Handoko Date: Sat, 17 Aug 2019 10:57:43 +0800 Subject: [PATCH 5/6] Switch Travis CI Java runtime to OpenJDK --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f751857..30f2542 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: scala -jdk: oraclejdk8 -scala: 2.12.2 +jdk: openjdk8 +scala: 2.12.9 env: global: - secure: "T284bq+EzkaCQOH12GyVt/WV3XOpr8JofrNwDQfImxE3PUjPPeTNrVId30uJmraunEIxEth1qxI+TL4WLjVGtKiNJTgsWq8OFaZy9eCD/hHtJV6vbQzEx2kUuJVROJjrxrpV+PqFnOYNpeyocrahwxKlvyksDWFnXMgPLJgzoqNMUamlZs9ZM+H7YnA3Sg81dYIHZDOmku0k36WZUKw6pF2kKv0MzLdrn9F9JpNX+oAH82yP4OCfoTInOHMcGBPqpebH1atxOeOQ/L4h17okpPWkyDsbK0OQzz2CbuaQDkfE++nDinFpU7R6IOLYNl29DGQATJX29bpfD7xOUWD7Z08Dx87sld+gEeQquECCoOHYDjH63/DmHrjVSXh9t94DptJPmrXQeI0kxAd5YNpoAuA41ilyJCJrRMeAoThxBwrWvxrkUvESTDLFbqlUd+ZYaP+nNoNPSYkFS3wYQCkOxUJ8zUSeJ8vkoPZwW49igRubUafBIbnd7ybU2AWx7hK1yzKzMe5Jlgiv/13gOBlAqSjfnQnh86HYpxpP1RoeMkpRQB5DGsi7C0Y5npG7eNCO5aeh2Uw6A3eIU4SjY3T7vVARswx0WKMS+Z72Euu7FGw5BuFhwRnP1H6CuBiC342eQIp3bS6VP4BxWyL7lpVgSYk806/LWi2xp4UGnnUzST4=" @@ -9,5 +9,6 @@ env: - MODULE_DIR=modules/play24 - MODULE_DIR=modules/play25 - MODULE_DIR=modules/play26 + - MODULE_DIR=modules/play27 script: cd $MODULE_DIR && ../../scripts/test.sh after_success: test $TRAVIS_BRANCH = "master" && ../../scripts/publish.sh From 76c925e5f4605babd2668fca8b0f97a7461cb30c Mon Sep 17 00:00:00 2001 From: Herdy Handoko Date: Sun, 18 Aug 2019 08:18:37 +0800 Subject: [PATCH 6/6] Update `scala-guice` version to latest for examples --- .../play24-example/app/com/hhandoko/ApplicationModule.scala | 2 +- examples/play24-example/build.sbt | 2 +- .../play25-example/app/com/hhandoko/ApplicationModule.scala | 2 +- examples/play25-example/build.sbt | 2 +- .../play26-example/app/com/hhandoko/ApplicationModule.scala | 2 +- examples/play26-example/build.sbt | 2 +- examples/play27-example/build.sbt | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/play24-example/app/com/hhandoko/ApplicationModule.scala b/examples/play24-example/app/com/hhandoko/ApplicationModule.scala index 9e80a38..eaceb46 100644 --- a/examples/play24-example/app/com/hhandoko/ApplicationModule.scala +++ b/examples/play24-example/app/com/hhandoko/ApplicationModule.scala @@ -34,7 +34,7 @@ import com.hhandoko.play.pdf.PdfGenerator class ApplicationModule extends AbstractModule with ScalaModule { /** Module configuration + binding */ - def configure(): Unit = {} + override def configure(): Unit = {} /** * Provides PDF generator implementation. diff --git a/examples/play24-example/build.sbt b/examples/play24-example/build.sbt index 172d652..b9b01ea 100644 --- a/examples/play24-example/build.sbt +++ b/examples/play24-example/build.sbt @@ -34,7 +34,7 @@ crossScalaVersions := Seq("2.10.7", "2.11.12") libraryDependencies ++= Seq( // Utilities - "net.codingwell" %% "scala-guice" % "4.1.0", + "net.codingwell" %% "scala-guice" % "4.1.1", // WebJars "org.webjars.bower" % "jquery" % "1.12.4", diff --git a/examples/play25-example/app/com/hhandoko/ApplicationModule.scala b/examples/play25-example/app/com/hhandoko/ApplicationModule.scala index e167e63..7c14d89 100644 --- a/examples/play25-example/app/com/hhandoko/ApplicationModule.scala +++ b/examples/play25-example/app/com/hhandoko/ApplicationModule.scala @@ -36,7 +36,7 @@ import com.hhandoko.play.pdf.PdfGenerator class ApplicationModule extends AbstractModule with ScalaModule { /** Module configuration + binding */ - def configure(): Unit = {} + override def configure(): Unit = {} /** * Provides PDF generator implementation. diff --git a/examples/play25-example/build.sbt b/examples/play25-example/build.sbt index 24fe1c9..03e1022 100644 --- a/examples/play25-example/build.sbt +++ b/examples/play25-example/build.sbt @@ -34,7 +34,7 @@ crossScalaVersions := Seq("2.11.12") libraryDependencies ++= Seq( // Utilities - "net.codingwell" %% "scala-guice" % "4.1.0", + "net.codingwell" %% "scala-guice" % "4.2.6", // WebJars "org.webjars.bower" % "jquery" % "1.12.4", diff --git a/examples/play26-example/app/com/hhandoko/ApplicationModule.scala b/examples/play26-example/app/com/hhandoko/ApplicationModule.scala index e167e63..7c14d89 100644 --- a/examples/play26-example/app/com/hhandoko/ApplicationModule.scala +++ b/examples/play26-example/app/com/hhandoko/ApplicationModule.scala @@ -36,7 +36,7 @@ import com.hhandoko.play.pdf.PdfGenerator class ApplicationModule extends AbstractModule with ScalaModule { /** Module configuration + binding */ - def configure(): Unit = {} + override def configure(): Unit = {} /** * Provides PDF generator implementation. diff --git a/examples/play26-example/build.sbt b/examples/play26-example/build.sbt index e2eedda..bbb8ab1 100644 --- a/examples/play26-example/build.sbt +++ b/examples/play26-example/build.sbt @@ -36,7 +36,7 @@ libraryDependencies ++= Seq( guice, // Utilities - "net.codingwell" %% "scala-guice" % "4.1.0", + "net.codingwell" %% "scala-guice" % "4.2.6", // WebJars "org.webjars.bower" % "jquery" % "1.12.4", diff --git a/examples/play27-example/build.sbt b/examples/play27-example/build.sbt index 644b523..f524765 100644 --- a/examples/play27-example/build.sbt +++ b/examples/play27-example/build.sbt @@ -36,7 +36,7 @@ libraryDependencies ++= Seq( guice, // Utilities - "net.codingwell" %% "scala-guice" % "4.1.0", + "net.codingwell" %% "scala-guice" % "4.2.6", // WebJars "org.webjars.bower" % "jquery" % "1.12.4", @@ -44,7 +44,7 @@ libraryDependencies ++= Seq( // ScalaTest + Play plugin // - http://www.scalatest.org/plus/play - "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.1" % Test + "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.3" % Test ) resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"