CSS未应用于生成的PDF

时间:2015-04-21 14:14:02

标签: java css flying-saucer

问题

我使用Flying-saucer从HTML文件生成PDF。

正确生成了PDF,但CSS未应用于它。

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
  <meta http-equiv="X-UA-Compatible" content="IE=8" />
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
  <link rel="stylesheet" media="print" type="text/css" href="C:/Users/f_antbar/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Gest_saisie/css/printPDF.css"/>
</head>
<body>
  <div style="background-color:white; width:100%"><img src="C:/Users/f_antbar/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Gest_saisie/img/logo_50.jpg"/></div>
  <div id="corps">...

发电机

try {
    FileOutputStream os = new FileOutputStream(context.getRealPath("documents")+"/print/test.pdf");
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(new File(context.getRealPath("documents")+"/print/test.html"));
    renderer.layout();
    renderer.createPDF(os);
    os.close();
    System.out.println("PDF created");
} catch (Exception ex) {
     ex.printStackTrace();
}

CSS由W3C验证器验证,路径是正确的,因为我只使用iText,但由于iText不支持我想用飞碟试用的CSS。

配置

我正在使用

  • Struts2的
  • iText 2.1.7
  • 飞碟核-9.0.7
  • 飞碟-PDF-9.0.7

1 个答案:

答案 0 :(得分:0)

****由于我无权添加评论,因此在答案部分

中提及

您好,

执行代码时,样式表中提到的href链接可能不是accessbile。通过URL提供,并使用相应的URL编辑html。 (确保代码正在执行的服务器可以访问它)。

要进行验证,请尝试将整个样式表代码插入<style></style>代码并执行。