I am having a challenge parsing a raw pdf file from frontend. The structure of the raw pdf file looks like this
%PDF-1.3 %���� 1 0 obj << /Type /Catalog /Pages 2 0 R /PageMode /UseThumbs>>endobj2obj<</Type /Pages/Kids [ 4 0 R ]/Count 1
I tried using ng2-pdf-viewer
and pdfjs-dist
but it seems like it only takes the location of the file and not the raw file as in binary like the example above.
I’ve attempted this using Angular, I’d appreciate any form of help.
here’s my code snippet.
<pdf-viewer [src]="pdfString" [show-all]="true"></pdf-viewer>
pdfString: string = '%PDF-1.7n%�І�n1 0 objn<</SubFilter/adbe.pkcs7.'
2
Answers
According to the documentation, if your PDF file is stored as a BASE64-encoded string, you could use
pdfSrc = atob(pdfString)
first to convert it to a binary string.In the other way, you can use
pdfSrc = btoa(pdfString)
if your pdf is stored a s a simple string.Then simply use
<pdf-viewer [src]="pdfSrc" [show-all]="true"></pdf-viewer>
The issue here is that
ng2-pdf-viewer
andpdfjs-dist
expect the file location or a structured data format as a source, not a raw string as you are trying to pass. To parse the raw PDF content you have, you can make use of theBlob
object. This object represents immutable raw binary data and can be used to display PDF content. Here’s how you can accomplish this using Angular andng2-pdf-viewer
:Blob
object.Blob
object.ng2-pdf-viewer
.This example takes a raw PDF string, encodes it in Base64, converts it to a
Uint8Array
, wraps it in aBlob
, and then creates an object URL that can be passed as the source tong2-pdf-viewer
. This should allow you to render the PDF in your Angular application.