├── project ├── build.properties └── release.sbt ├── version.sbt ├── example ├── logo-tommy.jpg ├── fonts │ ├── IndieFlower.ttf │ └── Lato-Hairline.ttf └── pdf.html ├── .gitignore ├── src ├── main │ └── scala │ │ └── net │ │ └── kaliber │ │ └── pdf │ │ ├── ClassLoaderUserAgent.scala │ │ └── PdfRenderer.scala └── test │ └── scala │ └── Test.scala ├── LICENSE └── README.md /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.5 -------------------------------------------------------------------------------- /version.sbt: -------------------------------------------------------------------------------- 1 | version in ThisBuild := "0.12" -------------------------------------------------------------------------------- /project/release.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.gseitz" % "sbt-release" % "0.8.5") -------------------------------------------------------------------------------- /example/logo-tommy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaliber-scala/scala-pdf/HEAD/example/logo-tommy.jpg -------------------------------------------------------------------------------- /example/fonts/IndieFlower.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaliber-scala/scala-pdf/HEAD/example/fonts/IndieFlower.ttf -------------------------------------------------------------------------------- /example/fonts/Lato-Hairline.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaliber-scala/scala-pdf/HEAD/example/fonts/Lato-Hairline.ttf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs 2 | project/project 3 | project/target 4 | target 5 | tmp 6 | .history 7 | .cache 8 | .classpath 9 | .project 10 | .settings/ 11 | .target/ 12 | dist 13 | bin/ 14 | .idea 15 | example/test.pdf 16 | worktest/* 17 | -------------------------------------------------------------------------------- /src/main/scala/net/kaliber/pdf/ClassLoaderUserAgent.scala: -------------------------------------------------------------------------------- 1 | package net.kaliber.pdf 2 | 3 | import org.xhtmlrenderer.layout.SharedContext 4 | import org.xhtmlrenderer.pdf.ITextOutputDevice 5 | import org.xhtmlrenderer.pdf.ITextUserAgent 6 | 7 | class ClassLoaderUserAgent( 8 | outputDevice: ITextOutputDevice, 9 | classLoader: ClassLoader, 10 | sharedContext: SharedContext 11 | ) extends ITextUserAgent(outputDevice) { 12 | 13 | setSharedContext(sharedContext) 14 | 15 | override protected def resolveAndOpenStream(uri: String) = 16 | Option(classLoader getResourceAsStream uri) getOrElse super.resolveAndOpenStream(uri) 17 | 18 | override protected def resolveURI(uri: String): String = uri 19 | } 20 | -------------------------------------------------------------------------------- /example/pdf.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 14 | 15 | 16 |
17 | test
18 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | Copyright (c) 2013 Jöerg Viola
3 |
4 | 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:
5 |
6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 |
8 | 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.
9 |
--------------------------------------------------------------------------------
/src/test/scala/Test.scala:
--------------------------------------------------------------------------------
1 | import java.net.URLClassLoader
2 | import java.nio.file.{Files, Path, Paths}
3 |
4 | import net.kaliber.pdf.PdfRenderer
5 | import org.qirx.littlespec.Specification
6 | import org.xhtmlrenderer.pdf.ITextRenderer
7 |
8 | import scala.io.Source
9 |
10 | object Test extends Specification {
11 |
12 | val examples: Path = Paths.get("./example")
13 | val `test.pdf`: Path = examples resolve "test.pdf"
14 |
15 | // Font should be rendered from anywhere
16 | val rendererWithFonts:ITextRenderer = new ITextRenderer
17 | rendererWithFonts.getFontResolver.addFontDirectory(examples.toAbsolutePath.toString + "/fonts", true)
18 |
19 | s"""|============================
20 | |PLEASE CHECK IF THE RENDERED
21 | |PDF CONTAINS TWO IMAGES AND
22 | |A CUSTOM FONT.
23 | |
24 | |${`test.pdf`}
25 | |============================""".stripMargin - {
26 | val classLoader = new URLClassLoader(Array(examples.toUri.toURL))
27 |
28 | val body = {
29 | val html = classLoader.getResourceAsStream("pdf.html")
30 | Source.fromInputStream(html).mkString
31 | }
32 |
33 | val pdf = {
34 | val renderer = new PdfRenderer(classLoader, rendererWithFonts)
35 | renderer.toBytes(body)
36 | }
37 |
38 | Files write (`test.pdf`, pdf)
39 |
40 | todo
41 |
42 | // Todo Write proper tests please
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/scala/net/kaliber/pdf/PdfRenderer.scala:
--------------------------------------------------------------------------------
1 | package net.kaliber.pdf
2 |
3 | import java.io.{ByteArrayOutputStream, StringReader, StringWriter}
4 |
5 | import org.w3c.tidy.Tidy
6 | import org.xhtmlrenderer.pdf.ITextRenderer
7 | import org.xhtmlrenderer.resource.XMLResource
8 | import org.xhtmlrenderer.context.StyleReference
9 |
10 | /**
11 | *
12 | * `PdfRenderer`
13 | *
14 | * a simple wrapper to generate content that could be rendered as pdf
15 | *
16 | * @param classLoader:ClassLoader - The class loader used to resolve assets
17 | * @param customRenderer:ITextRenderer - custom renderer to do changes on the fly
18 | */
19 | class PdfRenderer(classLoader: ClassLoader, customRenderer: ITextRenderer = new ITextRenderer) {
20 |
21 | private val renderer = doto(customRenderer) { renderer =>
22 | // spaghetti with bolognese
23 | val sharedContext = renderer.getSharedContext
24 | val userAgent = new ClassLoaderUserAgent(
25 | renderer.getOutputDevice,
26 | classLoader,
27 | sharedContext
28 | )
29 | sharedContext.setUserAgentCallback(userAgent)
30 | sharedContext.setCss(new StyleReference(userAgent))
31 | }
32 |
33 | private val tidy = doto(new Tidy)(_ setXHTML true)
34 |
35 | def toBytes(body: String): Array[Byte] =
36 | toStream(body).toByteArray
37 |
38 | def toStream(body: String): ByteArrayOutputStream =
39 | doto(new ByteArrayOutputStream) { output =>
40 | try {
41 | val reader = new StringReader(tidify(body))
42 | val document = XMLResource.load(reader).getDocument
43 | renderer.setDocument(document, "")
44 | renderer.layout()
45 | renderer.createPDF(output)
46 | } finally output.close()
47 | }
48 |
49 | private def tidify(body: String) =
50 | doto(new StringWriter) {
51 | tidy.parse(new StringReader(body), _)
52 | }.toString
53 |
54 | private def doto[T](t: T)(code: T => Unit): T = {code(t); t}
55 | }
56 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Scala PDF module
2 |
3 | This module helps in generating PDF documents dynamically from your Scala application.
4 | It simply renders your HTML- and CSS-based templates to PDF.
5 |
6 | It is based on the Flying Saucer library, which in turn uses iText for PDF generation.
7 |
8 | ## Installation
9 | ```
10 | libraryDependencies += "net.kaliber" %% "scala-pdf" % "0.12"
11 |
12 | resolvers += "Kaliber Repository" at "https://jars.kaliber.io/artifactory/libs-release-local"
13 | ```
14 |
15 | ## Usage
16 |
17 | ```
18 | val body = /* some xhtml string */
19 | val renderer = new PdfRenderer(classLoader)
20 | val bytes = renderer.toBytes(body)
21 | ```
22 |
23 | Please see the `Test.scala` file for an example of loading.
24 |
25 | ## Releases
26 |
27 | | 0.11 | 30 |2016-04-29 | 31 |Changed organisation | 32 |33 | |
| 0.10 | 35 |29.04.2016 | 36 |Radical rewrite | 37 |38 | |
| 0.9 | 41 |29.01.2016 | 42 |Set sharedContext on userAgent | 43 |44 | |
| 0.8 | 47 |27.04.2015 | 48 |Removed Play framework dependencies | 49 |50 | |
| 0.7 | 53 |19.02.2015 | 54 |Java => Scala | 55 |56 | |
| 0.6 | 59 |07.01.2015 | 60 |Play 2.3 | 61 |62 | |
| 0.5 | 65 |11.06.2013 | 66 |Fix with higher UTF-8 codes, documentBaseURL | 67 |Thanks Wolfert de Kraker | 68 |
| 0.4 | 71 |08.02.2013 | 72 |Play 2.1 | 73 |74 | |
| 0.4 | 77 |04.02.2013 | 78 |Play 2.1.RC4, remote images | 79 |80 | |
| 0.3 | 83 |15.06.2012 | 84 |CSS handling | 85 |86 | |
| 0.2 | 89 |21.05.2012 | 90 |Font handling | 91 |92 | |
| 0.1 | 95 |18.05.2012 | 96 |Initial release | 97 |98 | |