简单的解决方案:
如果你想快速测试,看看你的文件将与您的风格下工作(无需编写大量的代码将它集成到你的应用程序)。只需将您需要的CSS复制并粘贴到您的页面中即可。
越是工作方案
我的解决办法是阅读的CSS,并把它转换成html用预处理。由于这是一个较老的应用程序,可能不完全符合xhtml,我使用JSoup来加载html。下面的代码进行预处理。我会说,这些是代码片段,让你开始。伟大的事情,一旦你得到它的工作,你可以将您的服务器上的任何页面转换为PDF格式,无需任何额外的代码。在我的情况下,我设置了一个过滤器来查找特定的参数。如果该参数存在,我用请求包装器包装请求以访问html页面的最终呈现字节。然后我使用Jsoup解析它,然后pre =处理它。
/**this method uses JSOUP Document here is org.jsoup.nodes.Document
*/
@Override
public void modifyDOM(MyResourceResolver resources, Document normalizedDOM) {
//move style into head section
Elements styleTags = normalizedDOM.getElementsByTag("style");
normalizedDOM.select("style").remove();
for (org.jsoup.nodes.Element linkElement : styleTags) {
String curHead = normalizedDOM.head().html();
normalizedDOM.head().html(curHead + "\n" + linkElement.html() + "\n");
}
//now resolve css
Elements links = normalizedDOM.getElementsByTag("link");
for (org.jsoup.nodes.Element linkElement : links) {
String linkHref = linkElement.attr("href");
if (linkHref == null) {
linkHref = "";
}
String mediaSelector = linkElement.attr("media");
if (mediaSelector == null) {
mediaSelector = "";
}
mediaSelector = mediaSelector.trim();
if ("".equalsIgnoreCase(mediaSelector) || ("print".equalsIgnoreCase(mediaSelector))) {
byte[] contents = resources.getContentsOfHref(linkHref);
if (null != contents) {
//we've got the info let's add to the document as is
Tag styleTag = Tag.valueOf("style");
Attributes styleAttributes = new Attributes();
styleAttributes.put("type", "text/css");
String baseUri = "";
org.jsoup.nodes.Element styleElement = new Element(styleTag, baseUri, styleAttributes);
styleElement.text(new String(contents));
String curHead = normalizedDOM.head().html();
normalizedDOM.head().html(curHead + "\n<style type='text/css'>" + styleElement.html() + "</style>\n");
}
}
}
normalizedDOM.select("link").remove();
normalizedDOM.select("script").remove();
}
由于我插入的CSS和飞碟不支持JavaScript,我只是在预处理结束时从文档中删除这些引用。类MyResourceResolver只是我写的一个类,它引用了servlet上下文。实际读取服务器的css字节的方法看起来像这样:
public byte[] getContentsOfHref(String href) {
byte[] retval = null;
byte[] buf = new byte[8195];
int nread;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream is = null;
try {
if (href.startsWith("/myurlcontext")) {
href = href.substring("/myurlcontext".length());
}
is = context.getResourceAsStream(href);
while ((nread = is.read(buf)) >= 0) {
bos.write(buf, 0, nread);
}
retval = bos.toByteArray();
} catch (Exception ex) {
//do nothing for now
} finally {
try {
is.close();
} catch (Exception ex) {/*do nothing*/}
}
if (retval == null) {
System.out.println("Did not find: " + href);
} else {
System.out.println("Found: " + href + " " + retval.length + " bytes");
}
return retval;
}
下一个问题,如何初始化JSOUP Dom。嗯,我这样做,在读取所呈现的JSP页面的内容并把它传递给我的PDF生成代码的请求包装:
String renderedJSPString = new String(renderedJSP);
//these escape sequences are nuisance in xhtml.
renderedJSPString = renderedJSPString.replaceAll(" |©|&|<|>", "");
org.jsoup.nodes.Document parsedHtmlDOM = Jsoup.parse(renderedJSPString);
org.jsoup.nodes.Document normalizedDOM = parsedHtmlDOM.normalise();
normalizedDOM.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
normalizedDOM.outputSettings().prettyPrint(true);
...
preProcessor.modifyDOM(resolver, normalizedDOM);
...
将它添加到java默认cacerts修复它。 (--server.ssl.key-store仅适用于tomcat) – lilalinux 2018-03-06 10:06:53