Paris Karagiannopoulos
Paris Karagiannopoulos

Reputation: 591

Downloaded PDF from server is invalid although original is valid

I am using Omnifaces to create a pdf download button and get a pdf from the server. The downloaded pdf has blank pages and after using a pdf validator I am getting this errors:

Validating file "manual(6).pdf" for conformance level pdf1.7

The 'xref' keyword was not found or the xref table is malformed.

The file trailer dictionary is missing or invalid.

The "Length" key of the stream object is wrong.

Error in Flate stream: data error.

The "Length" key of the stream object is wrong.

The document does not conform to the requested standard.

The file format (header, trailer, objects, xref, streams) is corrupted.

The document does not conform to the PDF 1.7 standard.

Done.

My code works for other pdf files.

This is my code:

@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);
    }

}

and the xhtml:

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

The original pdf file scanned by the pdf validator return no errors. The pdf after downloaded returns the above errors.

Please help, thanks in advance!

Upvotes: 0

Views: 399

Answers (2)

Selaron
Selaron

Reputation: 6184

Another approach to provide a static file for dowload is to use the builtin resources system of JSF: See this Q/A for background.

For example place the Primefaces 6.2 Documentation into the /src/main/webapp/resources folder (note this is NOT the same as /src/main/resources in my other suggestion above!) so you have a file:

/src/main/webapp/resources/primefaces_user_guide_6_2.pdf

In your web project. Now in your faces simply add a static outputLink to this file:

<h:outputLink value="#{resource['primefaces_user_guide_6_2.pdf']}" >Download PF 6.2 Documentation!</h:outputLink>

That's it. The file will be served as is and the outputLink aktually provides a bookmarkable reference to the file.

This would btw. also circumvent the maven filtering issue as /src/main/webapp/resources normally should not be filtered.

Why two answers? Knowing I could have edited the first answer to include both suggestions, I'd like to know which one is accepted if any.

Upvotes: 2

Selaron
Selaron

Reputation: 6184

As your comment in a followup question suggests that maven corrupts your PDF during build of the .war archive, I'd suggest you have to disable maven resource filtering during build for PDF files in your POM.xml:

<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>

Upvotes: 3

Related Questions