0

I briefly summarize my problem: I'm calling an API that returns a pdf like

"% PDF-1.4%%1 0 obj << / Type / Catalog/ PageLayout / OneColumn/ Pages 2 0 R/ PageMode / UseNone ......... "

currently, I receive it in string format to be able to make changes and so far so good, but after making changes I would like to convert the string to blob to download the pdf. In doing this I am having problems, the text string converted to blob does not generate the correct pdf, or rather the pdf once opened is white, when in reality it should have data.

The code I'm using now is the following:

response.text().then((content) => {
   //...TODO: Modify pdf
   var blob = new Blob([content], { type: "application/pdf" });
   saveAs(blob, "invoice.pdf");
}).catch(error => {
   console.log(error);
});

The pdf is downloaded but if I open it it is empty.

I would like to be able to modify the pdf string and convert it back into a blob to be able to download it. Does anyone have an idea how I could do it?

YusufUMS
  • 1,506
  • 1
  • 12
  • 24
dbxdev90
  • 3
  • 2
  • I guess this is the same question :- https://stackoverflow.com/questions/25751017/how-to-load-a-pdf-into-a-blob-so-it-can-be-uploaded – Amol Gharpure Apr 27 '19 at 15:52
  • *"currently I receive it in string format"* - already there the pdf most likely gets damaged. A pdf is a binary file and handling it as a text file is very likely top damage it. Thus, never treat it as text. – mkl Apr 27 '19 at 15:54

1 Answers1

0

A PDF consists of a set of objects in a non-trivial fashion. If you are receiving it as a string and are using standard string manipulation functions on it, e.g. find and replace you are most likely going to corrupt it. You would have to edit in accord with the standards laid out in the PDF specification and not violate the syntax. This is a very fragile approach, you need to use a PDF library instead to edit your PDF content.

JosephA
  • 1,187
  • 3
  • 13
  • 27