benjohnde / play-pdf Goto Github PK
View Code? Open in Web Editor NEWA PDF module for the Play framework
License: MIT License
A PDF module for the Play framework
License: MIT License
Hi,
I'm having a hard time getting my styles to show on the generated PDF.
I've tried specifying the path using "@routes" as well as using the classpath:
<link rel="stylesheet" type="text/css" href="@routes.Assets.at("css/pdf.css")" media="screen" />
OR
<link rel="stylesheet" type="text/css" href="/public/css/pdf.css" media="screen" />
My stylesheet only contains one declaration:
td {
color: blue;
}
However, my PDF comes up with no styles applied. All text is black. Also tried changing background color and other properties.
Please let me know if you need more information. I really like the way this module integrates with Play! but I need styles to work for my client. Thanks!
As stated in issue #19, /conf/fonts directory must exist in order to avoid NullPointerException while rendering PDF. This can be fixed with simple check for folder existance. Anyway this issue has other consequences. PDF modules reads fonts directly from filesystem using Java API. This works flawlessly in dev mode using "play run", but if you publish your application to standalone package using "play dist", PDF module is not working anymore. When you create production-ready standalone package, the whole application is merged into the JAR file and every resource must be read through the classloader. Long story short, files are not available through the filesystem calls as desired folders do not exist any more. PDF module than results in NullPointerException every time because it is not able to find folder /conf/fonts. Actually, the feature for auto-loading fonts is unusable at all. The only way to load desired fonts is to use CSS and fetch fonts through the public assets directly from Play application.
Cyrillic letters are not displayed when I generate PDF.
Sample HTML - http://paste.ubuntu.com/5575893/
Generated PDF - http://dl.dropbox.com/u/3484004/temp/getPDF.pdf
Like I posted on your blog, this module is not ready for play 2.2.0
To make it usable on play 2.2.0
just edit your plugins.sbt:
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")
and your build.properties:
sbt.version=0.13.0
I have testet it locally with some simple prints, seems all to be working fine,
greets an thanks for your work ...
I have testet some simple Prints, seems all to be fine...
Hi,
The mandatory lookup of the directory /conf/fonts while setting up flying saucer should be optional.
If the user hasn't any specific fonts to add that only results in an empty folder created only to prevent runtime exception.
Thanks a lot !
I've added the appropriate lines to my Build.scala file;
val appDependencies = Seq(
"pdf" % "pdf_2.9.1" % "0.2"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
resolvers += Resolver.url("My GitHub Play Repository", url("http://www.joergviola.de/releases/"))(Resolver.ivyStylePatterns)
)
But when I restart play, I get some unresolved dependencies. Is this something I can work around?
[info] Set current project to select_account (in build file:/home/sam/select_account/)
[info] Updating {file:/home/sam/select_account/}select_account...
[info] downloading http://www.joergviola.de/releases/pdf/pdf_2.9.1/0.2/jars/pdf_2.9.1.jar ...
[info] [SUCCESSFUL ] pdf#pdf_2.9.1;0.2!pdf_2.9.1.jar (501ms)
[info] downloading http://repo.typesafe.com/typesafe/releases/org/xhtmlrenderer/core-renderer/R8/core-renderer-R8.jar ...
[info] [SUCCESSFUL ] org.xhtmlrenderer#core-renderer;R8!core-renderer.jar (1795ms)
[info] downloading http://repo.typesafe.com/typesafe/releases/com/lowagie/itext/2.0.8/itext-2.0.8.jar ...
[info] [SUCCESSFUL ] com.lowagie#itext;2.0.8!itext.jar (1018ms)
[info] downloading http://repo.typesafe.com/typesafe/releases/bouncycastle/bcmail-jdk14/138/bcmail-jdk14-138.jar ...
[info] [SUCCESSFUL ] bouncycastle#bcmail-jdk14;138!bcmail-jdk14.jar (487ms)
[info] downloading http://repo.typesafe.com/typesafe/releases/bouncycastle/bcprov-jdk14/138/bcprov-jdk14-138.jar ...
[info] [SUCCESSFUL ] bouncycastle#bcprov-jdk14;138!bcprov-jdk14.jar (941ms)
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: play#play_2.9.1;2.0.1: configuration not found in play#play_2.9.1;2.0.1: 'master'. It was required from pdf#pdf_2.9.1;0.2 compile
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[error] {file:/home/sam/select_account/}select_account/*:update: sbt.ResolveException: unresolved dependency: play#play_2.9.1;2.0.1: configuration not found in play#play_2.9.1;2.0.1: 'master'. It was required from pdf#pdf_2.9.1;0.2 compile
[error] java.util.NoSuchElementException: Either.right.value on Left
[error] Use 'last' for the full log.
Hi!
When application build by command: play dist, and run it, server response with main.css from module instead from my application.
How to tell play that it response with my main.css?
(Sorry for my english)
Hello Jörg,
I get an exception when invoking PDF.ok() in my Controller with Play version 2.1 and Play-pdf version 0.4.1.
Google indicates an issue between iText and Flying Saucer like here:
http://grails.1312388.n4.nabble.com/Grails-Rendering-Plugin-error-java-lang-NoSuchMethodError-com-lowagie-text-pdf-BaseFont-getCharBBox--td4525995.html
I appreciate any help or indications for a helpful source.
Thanks a lot!
Build.scala:
"pdf" % "pdf_2.10" % "0.4.1",
resolvers += Resolver.url("Violas Play Modules", url("http://www.joergviola.de/releases/"))(Resolver.ivyStylePatterns)
Below is my exception:
[error] application -
! @6dgga2n9a - Internal server error, for (GET) [/business/business-plan/result] ->
play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.NoSuchMethodError: com.lowagie.text.pdf.BaseFont.getCharBBox(C)[I]]
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.0]
at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:132) [play_2.10.jar:2.1.0]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:128) [play_2.10.jar:2.1.0]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.0]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.0]
java.lang.RuntimeException: java.lang.NoSuchMethodError: com.lowagie.text.pdf.BaseFont.getCharBBox(C)[I
at play.libs.F$Promise$6.apply(F.java:401) ~[play_2.10.jar:2.1.0]
at scala.concurrent.Future$$anonfun$map$1.liftedTree2$1(Future.scala:253) ~[scala-library.jar:na]
at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249) ~[scala-library.jar:na]
at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249) ~[scala-library.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29) ~[scala-library.jar:na]
at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.liftedTree1$1(BatchingExecutor.scala:67) ~[akka-actor_2.10.jar:na]
Caused by: java.lang.NoSuchMethodError: com.lowagie.text.pdf.BaseFont.getCharBBox(C)[I
at org.xhtmlrenderer.pdf.ITextFontResolver$FontDescription.setMetricDefaults(ITextFontResolver.java:679) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.pdf.ITextFontResolver$FontDescription.(ITextFontResolver.java:610) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.pdf.ITextFontResolver.addCourier(ITextFontResolver.java:410) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.pdf.ITextFontResolver.createInitialFontMap(ITextFontResolver.java:390) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.pdf.ITextFontResolver.(ITextFontResolver.java:52) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.pdf.ITextRenderer.(ITextRenderer.java:115) ~[core-renderer-R8.jar:na]
Scala 2.10 is out and Play 2.1 is at RC2 with release imminent.
Play PDF plugin support for the latest and greatest would be nice ;-)
Hi,
I can't use any color styling through css rendered in pdf.
I triedusing style attribute and class attribute with class defined in embedded style element, external stylesheet - on different kind of elements, div, span, p, table, etc.
No way.
Thanks a lot !
Sample program for pdf module is not working
2012-05-30 21:25:13,182 - [ERROR] - from application in play-akka.actor.actions-dispatcher-6
Creating document from template
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 7; The element type "link" must be terminated by the matching end-tag "".
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:79) ~[core-renderer-R8.jar:na]
at util.pdf.PDF.toStream(PDF.java:98) [pdf_2.9.1-0.2.jar:0.2]
at util.pdf.PDF.toBytes(PDF.java:84) [pdf_2.9.1-0.2.jar:0.2]
at util.pdf.PDF.ok(PDF.java:78) [pdf_2.9.1-0.2.jar:0.2]
Hi, it sounds that since an element in the XHTML template has an "id" attribute, the PDF generation fails:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<div id="wrapper">
<p>Hello world</p>
</div>
</body>
</html>
Console log:
InputStream: Doctype given is "-//W3C//DTD XHTML 1.0 Transitional//EN"
InputStream: Document content looks like XHTML 1.0 Strict
no warnings or errors were found
ERREUR : 'NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.'
[error] application - Creating document from template
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:79) ~[core-renderer-R8.jar:na]
at util.pdf.PDF.toStream(PDF.java:186) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.toBytes(PDF.java:168) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.toBytes(PDF.java:145) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.toBytes(PDF.java:140) [pdf_2.10-0.5.jar:0.5]
Caused by: javax.xml.transform.TransformerException: org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:732) ~[na:1.6.0_35]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336) ~[na:1.6.0_35]
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:79) ~[core-renderer-R8.jar:na]
at util.pdf.PDF.toStream(PDF.java:186) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.toBytes(PDF.java:168) [pdf_2.10-0.5.jar:0.5]
Caused by: org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
at org.apache.xerces.dom.ElementImpl.setIdAttributeNS(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
at com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM.startElement(SAX2DOM.java:201) ~[na:1.6.0_35]
at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.closeStartTag(ToXMLSAXHandler.java:204) ~[na:1.6.0_35]
at com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler.characters(ToXMLSAXHandler.java:524) ~[na:1.6.0_35]
at org.apache.xerces.parsers.AbstractSAXParser.characters(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
at org.apache.xerces.impl.dtd.XMLDTDValidator.characters(Unknown Source) ~[xercesImpl-2.8.1.jar:na]
Since I remove the "id" attribute, it works out finely. The XHTML template above is valid given http://validator.w3.org/check
Any chances?
There is zero documentation out there for how to install Play 2.0 modules. Some individual modules have installation instructions shown on their github page. This one does not. Help please!
Hi,
i have inserted an tag. In develop mode their is no problem. the images is shown all the time.
But in production mode, the images is not shown. And it dows not have effect to use an hosted image on another server. Same with @routes.Assets("images/....jpg")
Don´t know where the problem is.
I am using IntelliJ and sbt to create a dist.
Maybe switch to https://github.com/typesafehub/config or use dependency injection.
Hi!
Windows with jvm 1.7.0_21 works fine, but under linux (jvm 1.6.0_26) i've got an excepion with this stack trace:
java.io.IOException: Unsupported font type
at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:199) ~[core-renderer-R8.jar:na]
at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:157) ~[core-renderer-R8.jar:na]
at util.pdf.PDF.addFontDirectory(PDF.java:199) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.toStream(PDF.java:180) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.toBytes(PDF.java:168) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.toBytes(PDF.java:145) [pdf_2.10-0.5.jar:0.5]
at util.pdf.PDF.ok(PDF.java:135) [pdf_2.10-0.5.jar:0.5]
at controllers.OrderController.renderStatusPdf(OrderController.java:380) [classes/:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$13$$anonfun$apply$13.apply(routes_routing.scala:257) [classes/:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$13$$anonfun$apply$13.apply(routes_routing.scala:257) [classes/:na]
at play.core.Router$HandlerInvoker$$anon$6$$anon$2.invocation(Router.scala:175) [play_2.10.jar:2.1.2]
at play.core.Router$Routes$$anon$1.invocation(Router.scala:355) [play_2.10.jar:2.1.2]
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:50) [play_2.10.jar:2.1.2]
at play.db.jpa.TransactionalAction$1.apply(TransactionalAction.java:19) [play-java-jpa_2.10.jar:2.1.2]
at play.db.jpa.TransactionalAction$1.apply(TransactionalAction.java:17) [play-java-jpa_2.10.jar:2.1.2]
at play.db.jpa.JPA.withTransaction(JPA.java:101) [play-java-jpa_2.10.jar:2.1.2]
at play.db.jpa.TransactionalAction.call(TransactionalAction.java:14) [play-java-jpa_2.10.jar:2.1.2]
at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:80) [play_2.10.jar:2.1.2]
at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:79) [play_2.10.jar:2.1.2]
at play.libs.F$Promise$PromiseActor.onReceive(F.java:425) [play_2.10.jar:2.1.2]
at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:159) [akka-actor_2.10.jar:na]
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:425) [akka-actor_2.10.jar:na]
at akka.actor.ActorCell.invoke(ActorCell.scala:386) [akka-actor_2.10.jar:na]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:230) [akka-actor_2.10.jar:na]
at akka.dispatch.Mailbox.run(Mailbox.scala:212) [akka-actor_2.10.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:502) [akka-actor_2.10.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:262) [scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975) [scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1478) [scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104) [scala-library.jar:na]
I'm trying arial, calibri and cambria
play version 2.1.2
Regards,
Róbert
i just tried but I can not access the repo at http://www.joergviola.de/releases. Is this project still alive?
If I render the template as html I can render an image with the src attribute pointing to another server on my machine, i.e.
<img src="http://localhost/<path-to-file>/<filename> />
But if I render the PDF with PDF.ok(), I get the error pasted below and a 0 byte document. Using the class path in the src attribute works fine, but in my use case I have to get the images from another server outside of Play!
The documentation seems to indicate that it should be able to load images with an http request, but it doesn't appear to be working.
[error] application - Creating document from template java.util.NoSuchElementException: None.get at scala.None$.get(Option.scala:274) ~[scala-library.jar:0.11.3] at scala.None$.get(Option.scala:272) ~[scala-library.jar:0.11.3] at util.pdf.PDF$MyUserAgent.getImageResource(PDF.java:40) ~[pdf_2.9.1-0.2.jar:0.2] at org.xhtmlrenderer.pdf.ITextReplacedElementFactory.createReplacedElement(ITextReplacedElementFactory.java:57) ~[core-renderer-R8.jar:na] at org.xhtmlrenderer.render.BlockBox.calcDimensions(BlockBox.java:674) ~[core-renderer-R8.jar:na] at org.xhtmlrenderer.render.BlockBox.calcDimensions(BlockBox.java:628) ~[core-renderer-R8.jar:na] [info] Compiling 1 Java source to /home/sam/select_account/target/scala-2.9.1/classes...
Hi,
First, thank you for this module, that's a nice job (even if I could not make it work for now).
The module package is util.pdf.PDF
which is not a very good idea since there already is a util._
package with Scala (or Java).
I would thus recommend you to use something like de.joergviola.play20-pdf.pdf.PDF
instead. This would avoid confusion.
For example, at some place in my code, I use the util.Random
classes provided by Scala, which was not working anymore because of course there was no Random
in the PDF util
package.
Cheers and thanks
Is there a way to manage pages support using html (or any other way)?
i.e. is it possible to say, this markup goes in page 1, this other markup on page 2, etc?
Thank you
Gonzalo
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.