-3

I have an API (which I cannot tweak) that returns data blob of a PDF file to me. I need to transform it into an actual file and open it in a new tab in frontend.

I plan to use window.open(file_link, target='_blank') to open it, but I was given the actual content instead of a link. How do I write that data into a file and generate the link?

EDIT:

I followed the "solutions" in the suggested post but none works. My PDFstring is a source string apparently and base64 encoding doesn't do anything to it. (Chrome sees it as a url, actually. Pretty funny.)

I also tried using iframe + escape(PDFstring) which got me to open a new window, but two files I uploaded (which have no encryption) either asks me to provide a password or it simply shows a blank page

Whatever happened after that thread was answered made the thread invalid. The solutions don't work anymore.

EDIT2:

When using https://base64.guru/tools/repair to repair my string, it says Your string is not a Base64 value. It is a source of a PDF file.

I can directly call that API endpoint (through Postman) and it got me a download button and I can download the file, but when I want to use the same endpoint on frontend, it does return SOME data, but that data cannot be read and cannot be transformed into a PDF

EDIT3:

snippet of what that PDF data looks like

%PDF-1.3
%âãÏÓ
1 0 obj 
<<
/FormType 1
/Subtype /Form
/Resources 
<<
/Font 2 0 R
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/ExtGState 
<<
/gRLs0 
<<
/ca .15
>>
>>
>>
/Type /XObject
/BBox [0 0 595.28 841.89]
/Filter [/ASCII85Decode /FlateDecode]
/Length 254
/Matrix [1 0 0 1 0 0]
>>
stream
u^^Ü´¿ª.Æ}rP1   ó
ú@äùзeRØÏ¨L    a8  QÀ³©¢ícvôE-ñD²UAut,,íh~¬c®OÅ®k$»p_"½$ ~à£YpZ¿óFŤH¡PH¥é)ÂQ@*ÃdáÂ;qNLoâÀæ´#ßbÀw°è÷½­åóvQX}ç־9ÂÅp7ùÅ´àì-ËX
dTw ¦bbñ)ÐG$]ÿ°äÖñøÕÐ   ~CXvQûHõQf  þ~öþ+}¸eׯÞÇÚÁ´¶J´
endstream 
endobj 
2 0 obj 
<<
/F1 3 0 R
>>
endobj 
3 0 obj 
<<
/Encoding /WinAnsiEncoding
/Subtype /Type1
/Name /F1
/Type /Font
/BaseFont /Helvetica
>>
endobj 
4 0 obj 
<<
/Filter /FlateDecode
/Length 10
>>
stream
½Pa_bûûz
endstream 
endobj 
5 0 obj 
<<
/Filter /FlateDecode
/Length 46
>>
stream
/÷ÜUc^ Ù-Æ|daR «¯s¹*;HÒo#X»`²ª?Í
endstream 
endobj 

...

/Encoding /WinAnsiEncoding
/Subtype /Type1
/Name /F1
/Type /Font
/BaseFont /Helvetica
>>
endobj 
9 0 obj 
<<
/Filter /FlateDecode
/Length 10
>>
stream
².G`XÝ [
endstream 
endobj 
10 0 obj 
<<
/Filter /FlateDecode
/Length 46
>>

endobj xref
0 20
0000000000 65535 f 
0000000015 00000 n 
0000000567 00000 n 
0000000600 00000 n 
0000000709 00000 n 
0000000793 00000 n 
0000000913 00000 n 
0000001455 00000 n 
0000001488 00000 n 
0000001597 00000 n 
0000001681 00000 n 
0000001802 00000 n 
0000002045 00000 n 
0000061818 00000 n 
0000061680 00000 n 
0000061903 00000 n 
0000061939 00000 n 
0000064655 00000 n 
0000064708 00000 n 
0000064858 00000 n 
trailer

<<
/Info 19 0 R
/ID [<6fb9561b2e6dae8efff33dbd6911a492><28b448a6527da4b73885e10f7a1655c9>]
/Encrypt 18 0 R
/Root 17 0 R
/Size 20
>>
startxref
65027
%%EOF
JChao
  • 2,178
  • 5
  • 35
  • 65
  • Think this is what you want: https://stackoverflow.com/a/46064696/160419 – dezfowler Jul 09 '19 at 20:37
  • I saw that one. I couldn't get it to work, whether using iframe or not. still searching for a solution – JChao Jul 09 '19 at 20:43
  • It's saying the address `might be temporarily down or it may have moved permanently to a new web address`. It's not an address. It should be file data – JChao Jul 09 '19 at 20:45
  • Did you try this variation with a setTimeout? https://stackoverflow.com/questions/2805330/opening-pdf-string-in-new-window-with-javascript/46064696#comment100285196_46064696 – dezfowler Jul 09 '19 at 21:10
  • @dez yea, it opens an empty page instead. My `src` is not a link but PDF source data. If I take out `base64`, the iframe generates, but it shows data error – JChao Jul 09 '19 at 21:17
  • That solution isn't using a standard URL it is a data URL which contains the data of the resource itself base 64 encoded. What format is your data in? – dezfowler Jul 09 '19 at 21:53
  • @dez when i used `https://base64.guru/tools/repair` to repair the string, it tells me it's not a base64, it's a source of a PDF file. check my EDITs for more info. thanks! – JChao Jul 09 '19 at 21:56

1 Answers1

0

Okay, the problem is that my data is not encoded in base64. Once that data is encoded in base64, the answer provided in the suggested link worked.

JChao
  • 2,178
  • 5
  • 35
  • 65