尽管原始文件有效,但从服务器下载的 PDF 无效

我正在使用 Omnifaces 创建一个 pdf 下载按钮并从服务器获取 pdf。下载的 pdf 有空白页,使用 pdf 验证器后,我收到此错误:

验证文件“manual(6).pdf”的一致性级别 pdf1.7

未找到“外部参照”关键字或外部参照表格式错误。

文件尾字典丢失或无效。

流对象的“Length”键错误。

Flate 流中的错误:数据错误。

流对象的“Length”键错误。

该文件不符合要求的标准。

文件格式(标题、预告片、对象、外部参照、流)已损坏。

该文档不符合 PDF 1.7 标准。

完毕。

我的代码适用于其他 pdf 文件。

这是我的代码:

@ManagedBean

public class FileDownloadView {


    private static final String FILENAME = "manual.pdf";


    public void download() throws IOException {

        Resource resource = new ClassPathResource(FILENAME);

        File file = resource.getFile();

        Faces.sendFile(file, true);

    }


}

和xhtml:


<h:form>

    <p:commandButton action="#{fileDownloadView.download}" value="download" ajax="false">

    </p:commandButton>

</h:form>

pdf 验证器扫描的原始 pdf 文件没有返回错误。下载后的pdf返回上述错误。


请帮忙,提前谢谢!


小唯快跑啊
浏览 248回答 2
2回答

胡说叔叔

正如您在后续问题中的评论表明 maven 在构建 .war 存档期间损坏了您的 PDF,我建议您必须在构建 POM.xml 中的 PDF 文件期间禁用 maven 资源过滤:<resources>&nbsp; &nbsp; <resource>&nbsp; &nbsp; &nbsp; &nbsp; <directory>src/main/resources</directory>&nbsp; &nbsp; &nbsp; &nbsp; <filtering>true</filtering>&nbsp; &nbsp; &nbsp; &nbsp; <excludes>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <exclude>*.pdf</exclude>&nbsp; &nbsp; &nbsp; &nbsp; </excludes>&nbsp; &nbsp; </resource>&nbsp; &nbsp; <resource>&nbsp; &nbsp; &nbsp; &nbsp; <directory>src/main/resources</directory>&nbsp; &nbsp; &nbsp; &nbsp; <filtering>false</filtering>&nbsp; &nbsp; &nbsp; &nbsp; <includes>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <include>*.pdf</include>&nbsp; &nbsp; &nbsp; &nbsp; </includes>&nbsp; &nbsp; </resource></resources>

忽然笑

另一种为下载提供静态文件的方法是使用 JSF 的内置资源系统:有关背景信息,请参阅此 Q/A。例如,将 Primefaces 6.2 文档放入文件/src/main/webapp/resources夹中(注意这与我上面的其他建议不同!)所以你有一个文件:/src/main/resources/src/main/webapp/resources/primefaces_user_guide_6_2.pdf在您的网络项目中。现在在你的脸上只需添加一个静态输出链接到这个文件:<h:outputLink&nbsp;value="#{resource['primefaces_user_guide_6_2.pdf']}"&nbsp;>Download&nbsp;PF&nbsp;6.2&nbsp;Documentation!</h:outputLink>就是这样。该文件将按原样提供,并且 outputLink 实际上提供了对该文件的书签引用。这会顺便说一句。还可以规避 maven 过滤问题,因为 /src/main/webapp/resources 通常不应该被过滤。为什么要两个答案?知道我可以编辑第一个答案以包含这两个建议,我想知道哪个被接受(如果有的话)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java