0

I can generate a PDF using flying-saucer/iText. However, I can't figure out how to get the style sheet included in the PDF rendering.

I have used this flying-saucer/iText PDF in servlet not finding css file as reference.

There are multiple css files involved so I won't be able to use renderer.setDocument(doc, "http://example.com/something/page.html"); as a solution

I have implemented a something similar to what the asker used, but it's returning a Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 4: http://localhost:8080/proj/resources/css/custom1.css error

Here is my code

      StringBuilder bui = new StringBuilder();
      bui.append("<html><head><style>");
      bui.append(readFile(path+"/resources/css/custom1.css", Charsets.UTF_8));
      bui.append(readFile(path+"/resources/css/custom2.css", Charsets.UTF_8));
      bui.append(readFile(path+"/resources/css/custom3.css", Charsets.UTF_8));

      bui.append("</style></head>");
      bui.append("<body><div><table>");
      bui.append( xhtml_file );
      bui.append("</table></div></body></html>");

      InputStream stream = new ByteArrayInputStream(bui.toString().getBytes( StandardCharsets.UTF_8 ));
      Document doc = tidy.parseDOM(stream, null);
      File outputFile = new File(directory+ "FILENAME" +".pdf");
      os = new FileOutputStream(outputFile);
      ITextRenderer renderer = new ITextRenderer();
      renderer.setDocument(doc, null);
      renderer.layout();
      renderer.createPDF(os, false);
      renderer.finishPDF();

the path being passed is "http://localhost:8080" if I enter "http://localhost:8080/resources/css/custom1.css" directly into the address bar, it shows the css file. I tried removing the 'path' but it's also not getting the css. What am I doing wrong?

Community
  • 1
  • 1
bismarck
  • 1
  • 4

1 Answers1

0

Try the file:/// url protocol to point (with three slashes) pointing to the css file directly.

JoSeTe4ever
  • 473
  • 6
  • 17