Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XHTML element with attribute "id" throws an error #17

Closed
teemoo7 opened this issue Jul 5, 2013 · 9 comments
Closed

XHTML element with attribute "id" throws an error #17

teemoo7 opened this issue Jul 5, 2013 · 9 comments

Comments

@teemoo7
Copy link

teemoo7 commented Jul 5, 2013

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

@dmounoud
Copy link

Because the iText dependency is old.

@teemoo7 use this dependency import instead:

("pdf" %% "pdf" % "0.5").exclude("org.xhtmlrenderer", "core-renderer"),
"org.xhtmlrenderer" % "flying-saucer-pdf" % "9.0.2"

I exclude the one embedded in this library and specify the latest saucer release.

Should be better to update the project build config.

Cheers

@teemoo7
Copy link
Author

teemoo7 commented Jul 11, 2013

@diadys Thank you for the advice, your solution probably improves the general plugin behaviour but it still does not work with the ID attribute bug. Same error as before. Any help is welcome!

@godenji
Copy link

godenji commented Oct 11, 2013

@teemoo7 did you ever find a workaround for this? Hitting same issue with Play 2.2

@godenji
Copy link

godenji commented Oct 12, 2013

For anyone getting bitten by the "id" problem, Flying Saucer PDF author has hard coded namespaces to true:
http://grepcode.com/file/repo1.maven.org/maven2/org.xhtmlrenderer/core-renderer/R8/org/xhtmlrenderer/resource/XMLResource.java#238

Apparently this is, for some reason, what breaks

....
based markup; see xerces user group:
https://groups.google.com/d/msg/flying-saucer-users/3nzExUJ3kfY/qcnu_B3N6acJ

I spent yesterday afternoon trying to work around the issue, but none of the configuration overrides took, the -Dxr java flag in particular.

Anyway, my current use case for the plugin is generating customer invoices from a single template, so going with replacing "id" with "class" as the quick & dirty workaround for now.

@teemoo7
Copy link
Author

teemoo7 commented Oct 14, 2013

@diadys Unfortunately no, no good workaround. The only thing I do now, like @godenji , is to use the "class" attribute instead of the "id" attribute. Not perfect, but works out finely.

@fhars
Copy link
Contributor

fhars commented Nov 29, 2013

I see the exception with play 2.1.5, but the exact same source code (with id attributes in the pdf templates) works like a charm if run with play 2.1.3.

@godenji
Copy link

godenji commented Jul 10, 2014

@teemoo7 @fhars @diadys and anyone else looking to use css id selectors to generate PDFs, start Play with the following flag:

-Dxr.load.configure-features=true

Without the above, namespace support is hard coded to true, which for some reason (Xerces?) breaks PDF generation when id selectors are present in one's XHTML markup.

@eweiguu
Copy link

eweiguu commented Dec 22, 2016

another way to work around is try to parse the xml document and then pass it to flying-saucer

@benjohnde
Copy link
Owner

I updated all the dependencies to the most recent version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants