我一直在尝试加载位于"/resources/ PDF /“的pdf文件。我想加载pdf,填充表单域并返回一个流。到目前为止,这是有效的,没有错误或异常。问题是,在打印生成的PDF时,文档的某些部分会丢失。使用this pdf,它只打印表单域,而不打印图像或文本。代码运行在与primefaces相结合的tomcat7中:
public StreamedContent modify() {
String pdfFile = "mypdf.pdf";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
InputStream istream = getClass().getResourceAsStream("/pdf/" + pdfFile);
PdfReader reader = new PdfReader(istream);
pdfStamper = new PdfStamper(reader, bos );
pdfForm = pdfStamper.getAcroFields();
// fillData();
pdfStamper.close();
reader.close();
istream.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
bis.close();
bos.close();
return new DefaultStreamedContent( bis, "application/pdf", "report.pdf" );
} catch (Exception ex) {
ex.printStackTrace();
return null;
}我是这样构建项目的:mvn clean install tomcat7:redeploy -DskipTests
你知道哪里出问题了吗?谢谢。
发布于 2019-04-24 20:22:26
更新:我刚刚遇到了同样的问题!经过深入的研究,我发现maven破解了我的PDF文件的编码。我应该更仔细地阅读MKLs的评论;-)
我在maven项目中添加了资源插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<nonFilteredFileExtensions>
<!-- Please note that images like jpg, jpeg, gif, bmp and png are (already) implicitly excluded -->
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>老帖子:
你的帖子缺少重要信息:
(打印时出现问题,仅打印?)
其中一个可能是错误的地方:
您没有设置流的编码(例如UTF-8):
return new DefaultStreamedContent( bis, "application/pdf", "report.pdf", "YourEncoding");
顺便说一句,原始的PDF也有错误(例如,印前检查报告了几个错误)。
发布于 2019-04-24 22:52:07
我最终决定用另一种方式来做这件事。
在项目属性文件中,我添加了一个带有PDF所在路径的新属性,这样我就可以通过新的FileInputStream最终代码加载带有文件的pdfReader对象
public StreamedContent modify() {
File file = getPdfFile();
PdfReader reader = new PdfReader(new FileInputStream(file));
pdfStamper = new PdfStamper(reader, bos );
// fillData();
pdfStamper.close();
bos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return new DefaultStreamedContent( bis, "application/pdf", "report.pdf" );
}
public File getPdfFile() {
try {
Properties prop = new Properties();
prop.load(getClass().getClassLoader()
.getResourceAsStream("myfile.properties"));
String pdfPath = prop.getProperty("pdf.path");
String pdfName = prop.getProperty("pdf.name");
File file = new File(pdfPath + pdfName);
return file;
} catch (Exception ex) {
LOGGER.error("ERROR: " + ex.getMessage());
return null;
}
}非常感谢!致以敬意,
https://stackoverflow.com/questions/55810205
复制相似问题