-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItextBug.scala
More file actions
28 lines (22 loc) · 998 Bytes
/
ItextBug.scala
File metadata and controls
28 lines (22 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.{ByteArrayInputStream, FileOutputStream, OutputStream}
import java.nio.file.{Files, Paths}
import com.itextpdf.html2pdf.HtmlConverter
import com.itextpdf.kernel.geom.PageSize
import com.itextpdf.kernel.pdf.{PdfDocument, PdfWriter}
// Run this from the directory containing TEST.html
object ItextBug extends App {
def saveAsPdf(out: OutputStream, html: String, orientation: String): Unit = {
val pageSize = if (orientation == "landscape") PageSize.LETTER.rotate() else PageSize.LETTER
val writer: PdfWriter = new PdfWriter(out)
val document: PdfDocument = new PdfDocument(writer)
document.setDefaultPageSize(pageSize)
HtmlConverter.convertToPdf(new ByteArrayInputStream(html.getBytes()), document)
out.close()
}
val html = new String(Files.readAllBytes(Paths.get("TEST.html")))
val out = new FileOutputStream("TEST.pdf")
// This version crashes
saveAsPdf(out, html, "landscape")
// This version works
// saveAsPdf(out, html, "portrait")
}