1

I'm using the following to convert HTML to PDF:

InputStream convert(InputStream fileInputStream) {

        PipedInputStream inputStream = new PipedInputStream()
        PipedOutputStream outputStream = new PipedOutputStream(inputStream)
        new Thread({
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(false);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(fileInputStream)

            ITextRenderer renderer = new ITextRenderer()
            renderer.setDocument(document, "")
             renderer.layout()
            renderer.createPDF(outputStream)
        }).start()

        return inputStream
    }

From the documentation, apparently I should be able to set a "User Agent" resolver somewhere, but I'm not sure where, exactly. Anyone know how to ignore external CSS in a document?

bluish
  • 26,356
  • 27
  • 122
  • 180
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406

1 Answers1

1

Not the same question but my answer for that one will work here too: Resolving protected resources with Flying Saucer (ITextRenderer)

Override this method:

public CSSResource getCSSResource(String uri) {
    return new CSSResource(resolveAndOpenStream(uri));
}

with

public CSSResource getCSSResource(String uri) {
    return new CSSResource(new ByteArrayInputStream([] as byte[]));
}
Community
  • 1
  • 1
Adam
  • 5,045
  • 1
  • 25
  • 31
  • How do I not resolve? I tried retuning null, but that gives a nullpointerexception. java.lang.NullPointerException at org.xhtmlrenderer.context.StylesheetFactoryImpl.parse(StylesheetFactoryImpl.java:93) – Stefan Kendall Mar 15 '11 at 17:32
  • Yes, as you updated you have to return a CSSResource with an empty InputStream. – Adam Mar 15 '11 at 19:07