io.github.cloudify.scala.spdf.SourceDocumentLike.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spdf_2.9.3 Show documentation
Show all versions of spdf_2.9.3 Show documentation
Create PDFs using plain old HTML+CSS. Uses wkhtmltopdf on the back-end which renders HTML using Webkit.
The newest version!
package io.github.cloudify.scala.spdf
import java.io.{ByteArrayInputStream, InputStream, File}
import scala.sys.process._
import java.net.URL
import scala.annotation.implicitNotFound
import scala.xml.Elem
/**
* Type class that describes the kind of source documents we can read the
* input HTML from.
*/
@implicitNotFound(msg = "Cannot find SourceDocumentLike type class for ${A}")
trait SourceDocumentLike[-A] {
/**
* The source parameter to provide to `wkhtmltopdf`
* Defaults to read from STDIN.
*/
def commandParameter(sourceDocument: A): String = "-"
/**
* Source the input of the process from this sourceDocument
* Defaults to passthrough.
*/
def sourceFrom(sourceDocument: A)(process: ProcessBuilder): ProcessBuilder =
process
}
object SourceDocumentLike {
/**
* Pipes the InputStream into the process STDIN
*/
implicit object InputStreamSourceDocument extends SourceDocumentLike[InputStream] {
override def sourceFrom(sourceDocument: InputStream)(process: ProcessBuilder): ProcessBuilder =
process #< sourceDocument
}
/**
* Sets the file absolute path as the input parameter
*/
implicit object FileSourceDocument extends SourceDocumentLike[File] {
override def commandParameter(sourceDocument: File): String =
sourceDocument.getAbsolutePath
}
/**
* Pipes a UTF-8 string into the process STDIN
*/
implicit object StringSourceDocument extends SourceDocumentLike[String] {
override def sourceFrom(sourceDocument: String)(process: ProcessBuilder) =
process #< toInputStream(sourceDocument)
private def toInputStream(sourceDocument: String): ByteArrayInputStream =
new ByteArrayInputStream(sourceDocument.getBytes("UTF-8"))
}
/**
* Sets the URL as the input parameter
*/
implicit object URLSourceDocument extends SourceDocumentLike[URL] {
override def commandParameter(sourceDocument: URL) = sourceDocument.getProtocol match {
case "https" | "http" | "file" => sourceDocument.toString
case _ => throw new UnsupportedProtocolException(sourceDocument)
}
}
/**
* Sets the XML node as the input parameter
*/
implicit object XmlSourceDocument extends SourceDocumentLike[Elem] {
override def sourceFrom(sourceDocument: Elem)(process: ProcessBuilder) =
process #< toInputStream(sourceDocument)
private def toInputStream(sourceDocument: Elem): ByteArrayInputStream =
new ByteArrayInputStream(sourceDocument.toString().getBytes("UTF-8"))
}
}