首页 > 解决方案 > 尽管原始文件有效,但从服务器下载的 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返回上述错误。

请帮忙,提前谢谢!

标签: javapdfomnifaces

解决方案


正如您在后续问题中的评论表明 maven 在构建 .war 存档期间损坏了您的 PDF,我建议您必须在构建 POM.xml 中的 PDF 文件期间禁用 maven 资源过滤:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>*.pdf</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <includes>
            <include>*.pdf</include>
        </includes>
    </resource>
</resources>

推荐阅读