javascript - pdf.js giving back "InvalidPDFException: Invalid Pdf Structure" - Stack Overflow

I currently have a backend java service that converts html to pdfs for me using itext.I return the pd

I currently have a backend java service that converts html to pdfs for me using itext. I return the pdf as a byte[] just fine to the client(Angular 5). But I end up getting "invalid pdf structure" when I try running the getDocument function on it. I do not believe that the pdf structure is actually invalid. I uploaded my html template to an online pdf converter and it worked just fine.

This is what I'm getting from the backend:

This is my pdfjs code:

class MyDocumentsProvider{
      downloadPdf():any{
    return this.http.get(environment.webappServer+"/get1098E", {responseType:'arraybuffer'}).map(
      (res) =>{
          return res;
        }
      )
  }
}
this.myDocumentsProvider.downloadPdf().subscribe((res)=>{
        PDFJS.disableWorker = true; //<-- removing this does nothing

        PDFJS.getDocument(res).then((pdf)=>{
          this.showLoader = false;
            this.pdf = pdf;
            this.pagesTotal = pdf.numPages;
            pdf.getPage(this.pageNum).then((page) => {
                this.handlePages(page);
          this.writeFile();
            })
        }).catch((err)=>{
            this.showError = true;
        console.error(err);
        })
    },((err)=>{
      this.showError = true;
      console.error(err);
    }))
}

I've also tried doing

    PDFJS.getDocument(new Uint8Array(res))then((pdf)...

I have tested this code with direct url to a pdf file and it works.

This is some java code:

@RequestMapping(value="/testPdf",  headers="Accept=*/*", method = RequestMethod.GET)
    public ResponseEntity<?> testPdf() throws IOException{


        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type","application/octet-stream;charset=UTF-8"); //<-- this was added later on.  Did nothing....
        ResponseEntity <byte[]> arr = pdfService.htmlTemplateToPdf()
        return new ResponseEntity<>(arr,responseHeaders, HttpStatus.OK);

    }

Please lend me your assistance fellow SOs!

I currently have a backend java service that converts html to pdfs for me using itext. I return the pdf as a byte[] just fine to the client(Angular 5). But I end up getting "invalid pdf structure" when I try running the getDocument function on it. I do not believe that the pdf structure is actually invalid. I uploaded my html template to an online pdf converter and it worked just fine.

This is what I'm getting from the backend:

This is my pdfjs code:

class MyDocumentsProvider{
      downloadPdf():any{
    return this.http.get(environment.webappServer+"/get1098E", {responseType:'arraybuffer'}).map(
      (res) =>{
          return res;
        }
      )
  }
}
this.myDocumentsProvider.downloadPdf().subscribe((res)=>{
        PDFJS.disableWorker = true; //<-- removing this does nothing

        PDFJS.getDocument(res).then((pdf)=>{
          this.showLoader = false;
            this.pdf = pdf;
            this.pagesTotal = pdf.numPages;
            pdf.getPage(this.pageNum).then((page) => {
                this.handlePages(page);
          this.writeFile();
            })
        }).catch((err)=>{
            this.showError = true;
        console.error(err);
        })
    },((err)=>{
      this.showError = true;
      console.error(err);
    }))
}

I've also tried doing

    PDFJS.getDocument(new Uint8Array(res))then((pdf)...

I have tested this code with direct url to a pdf file and it works.

This is some java code:

@RequestMapping(value="/testPdf",  headers="Accept=*/*", method = RequestMethod.GET)
    public ResponseEntity<?> testPdf() throws IOException{


        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type","application/octet-stream;charset=UTF-8"); //<-- this was added later on.  Did nothing....
        ResponseEntity <byte[]> arr = pdfService.htmlTemplateToPdf()
        return new ResponseEntity<>(arr,responseHeaders, HttpStatus.OK);

    }

Please lend me your assistance fellow SOs!

Share Improve this question asked Apr 16, 2018 at 3:11 jackjoesmithjackjoesmith 9715 gold badges21 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Instead of :

return this.http.get(environment.webappServer+"/get1098E", {responseType:'arraybuffer'}).map(
  (res) =>{
      return res;
    }
  )

I just removed the responseType:'arraybuffer'. Then I took the response and manually converted it into a typed array (Uint8Array). I guess responseType:arraybuffer was giving back something corrupted.

For me, I was trying to load a PDF via the data parameter but feeding in base64 directly. As you docs say, you need to convert to binary before loading. Historically this is done with atob() however it is better now to get the binary representation directly from whatever is supplying it

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742339331a4425300.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信