'How to convert raw PDF to PDF Document

Hello there I tried to convert raw PDF to PDF to display.
I am currently using react-pdf module to display and it works well with my local PDF file like below.
saved pdf file


The thing is most of PDF viewer modules need a path or imported file(from local or cloud) description of file formatbut I only can get a raw PDF from backend means they give a PDF file itself(not a path). looks like below.

    prescription %PDF-1.7
%����
1 0 obj
<</Pages 2 0 R /Type/Catalog>>
endobj
2 0 obj
<</Count 6/Kids[ 4 0 R  47 0 R  59 0 R  64 0 R  69 0 R  74 0 R ]/Type/Pages>>
endobj
3 0 obj
<</CreationDate(D:20211007171608)/Creator(PDFium)/Producer(PDFium)>>
endobj
4 0 obj
<</BleedBox[ 0 0 595 841]/Contents 5 0 R /MediaBox[ 0 0 595 841]/Parent 2 0 R /Resources 6 0 R /TrimBox[ 0 0 595 841]/Type/Page>>
endobj
5 0 obj
<</Filter/FlateDecode/Length 21432>>stream
x�ݽ͎$ɒ��ϧ�5�km�g�
j1�� �Yp�� ����"Adѽ����3���V��
��@�Kf|�n�&v\�������-��O_K�[k���پo!�Z�'������������_�c)���R��%�������-?���^�����7s�0�\j}�k������Uҿ��߾~���|��[���֯��n_?�?�Ç�{*�K��{��v��j    �=}ٿ�{���_�Ͽ�����P�_���/�����k�R~6��/��������.����?����K�ʳ ������������;���>K���ϒ�9��������j����<�g�{ͷ?K���q��?�����y�D���PO�O_��_��VK���/��7�1�����1�H��r����{m��U<�"��ELGyw��n)\�T  y�c5�����z���c�|���u|����������g�cq;�t]�?�u\����ť ���J

I guess I need to save this raw code in my local(I think this is not a good way) or convert this raw pdf directly to display(raw -> base64 -> ?)
So I tried convert this raw to base64 but keep throwing error like this is not a right PDF format..

The last way is asking for uploading pdf on cloud server(to get url) to backend...

Is someone can give any idea or hint? This torturing me a lot..... >->-o
Thanks for reading my question.



Solution 1:[1]

you can convert PDF streaming data into blob .

// Create a Blob from the PDF Stream
const file = new Blob([pdf response data], { type: 'application/pdf' }) 
// Build a URL from the file  
const fileURL = URL.createObjectURL(file)
// Open the URL on new Window
window.open(fileURL)

one more point , please make sure to pass response type header as arraybuffer

header['responseType'] = 'arraybuffer' 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rajesh Parmar