flying-saucer/iText PDF in servlet not finding css file

房东的猫 提交于 2019-12-04 14:31:46

There are multiple ways to solve this, one is yours, others are:

1: Set the url for your document. You call renderer.setDocument(doc, null); the second parameter is the base url, resources will be located relateive to that.

An example:

  • Your document has <link href="my.css" ..
  • The css is located at http://example.com/something/my.css
  • You should call renderer.setDocument(doc, "http://example.com/something/page.html");

2: Implement the UserAgentCallback interface and set it with renderer.getSharedContext().setUserAgentCallback(myUserAgentCallback);

Zack Macomber

I decided with just reading my CSS file on the server side into a String.

The readFile method is based off of Jon Skeet's post at How do I create a Java string from the contents of a file?):

buf.append("<head><style>");
buf.append(readFile(getServletContext().getRealPath("/PDFservlet.css"), "UTF-8"));
buf.append("</style></head>");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!