question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get warning Invalid stream: "FormatError: Bad FCHECK in flate stream: 120, 253"

See original GitHub issue

HI, I have a Jersey Backend REST service to get the PDF from server to Client. I set the response to React state so that this will not retrieve the PDF from backend again in state changed. However, I got the error Warning: Invalid stream: “FormatError: Bad FCHECK in flate stream: 120, 253” in loading that PDF to Document. All content of PDF are lost.

May I know how to solve this?

Below is my code:

Server

        @Override
        @GET
        @Path("/pdf")
        @Produces(MediaType.APPLICATION_PDF_VALUE)
public Response testPdf() throws Exception {

    File file = new File("C:\\Desktop\\test.pdf");
    FileInputStream fileInputStream = new FileInputStream(file);

    ResponseBuilder response = Response.ok((Object) fileInputStream);
    response.type("application/pdf");
    response.header("Content-Disposition", "filename=test.pdf");

    return response.build();
}

Client

        import React, { Component } from 'react'; 
        import { Document, Page } from 'react-pdf';
        import axios from 'axios';

      class MyApp extends Component {
         state = {
             numPages: null,
            pageNumber: 1,
           pdfContent: null
       }

    componentDidMount(){
        var that = this;

        axio.get("url\Pdf).then((response) => {
             that.setState({pdfContent: response });
        }).catch((error) => {
             console.warn(error);
        });
    }

    onDocumentLoadSuccess = ({ numPages }) => {
        this.setState({ numPages });
    }

       printHandler(){
           window.print();
      }

      render() {
          const { pageNumber, numPages } = this.state;

      return (
          <div>
             <Document
                file={this.state.pdfContent}
                onLoadSuccess={this.onDocumentLoadSuccess}
             >
                 <Page pageNumber={pageNumber} />
             </Document>
             <p>Page {pageNumber} of {numPages}</p>

             <button onClick={() => this.setState(prevState => ({ 
                     pageNumber: prevState.pageNumber + 1 }))}>Next page</button>
             <button onClick={() => this.setState(prevState => ({ 
                     pageNumber: prevState.pageNumber - 1 }))}>Prev Page</button>

              <button onClick={this.printHandler}/>
          </div>
      );
} }
```

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

30reactions
depictioncommented, Feb 13, 2019

As mentioned above, make sure that the response type is set to ‘blob’. Here is an example using Axios that calls an API, sets the response type, and creates a blob url from the response.

export function myService () {
    return axios.get('https://www.domain.com/api/, {
        responseType: 'blob',
        transformResponse: [function (data) {
            let blob = new window.Blob([data], { type: 'application/pdf' })
            return window.URL.createObjectURL(blob)
        }]
    })
}
23reactions
LukaszGrelacommented, Feb 13, 2019

Thanks, I’ve in the end set the responseType:'arraybuffer' in axios config and then passed the response.data to the Document’s file prop as object {data:...}

    Axios.get('http://path/to/api', { responseType: 'arraybuffer' }).then(
      response => {
        this.setState(_ => ({ file: { data: response.data } }));
      }
    );

which then is used in Document

<Document file={this.state.file}
//...
Read more comments on GitHub >

github_iconTop Results From Across the Web

"FormatError: Bad FCHECK in flate stream: 120, 239" on PDF ...
I know that the pdf itself is not corrupt, because when I make a get request to the API to retrieve the stored...
Read more >
How can I load PDF from from a response? : r/learnjavascript
This however doesn't work. I get an error that says: Warning: Invalid stream: "FormatError: Bad FCHECK in flate stream: 120, 239".
Read more >
Display PDF loaded from server - - Bountysource
[x] I have read documentation in README; [x] I have checked sample and test suites to see real life basic implementation; [x] I...
Read more >
FormatError: Bad FCHECK in flate stream - pdf.js出现bug
pdf.js出现bug:Warning: Invalid stream: “FormatError: Bad FCHECK in ... 情况自行修改 } // get请求映射params参数 if (config.method === 'get' ...
Read more >
[jira] [Comment Edited] (PDFBOX-4781) PDF files with invalid ...
... "FormatError: Bad FCHECK in flate stream: 120, 194" Warning: ... Object 9 is said to have a length of 37, but has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found