Error when loading PDF from base64 string: "PDF is missing a header"
Hi @Hopding! First off, thanks so much for writing pdf-lib. It's really great stuff.
I'm just getting an error of "message": "PDF is missing a header" when trying to modify a pdf from a fetch. I'm more or less just using your example and a convertStringPdfToBinary I found in another thread of yours.
Any help would be most welcome. :)
const {
PDFDocumentFactory,
PDFDocumentWriter,
drawLinesOfText,
StandardFonts,
} = require('pdf-lib');
const { decode } = require('base64-arraybuffer');
const BASE64_MARKER = ';base64,';
const convertStringPdfToBinary = (pdfString) => {
const base64Index = pdfString.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
const base64 = pdfString.substring(base64Index);
const raw = decode(base64);
const uint8Array = new Uint8Array(raw.length);
let i;
for (i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
};
// edit an existing pdf
const editPdf = (pdf) => {
const pdfUint8Array = convertStringPdfToBinary(pdf);
const pdfDoc = PDFDocumentFactory.load(pdfUint8Array);
const COURIER_FONT = 'Courier';
const [courierRef, courierFont] = pdfDoc.embedStandardFont(
StandardFonts.Courier
);
const pages = pdfDoc.getPages();
const existingPage = pages[0].addFontDictionary(COURIER_FONT, courierRef);
const newContentStream = pdfDoc.createContentStream(
drawLinesOfText(
['This text was added', 'with JavaScript!'].map(courierFont.encodeText),
{
x: 30,
y: 150,
font: COURIER_FONT,
size: 48,
colorRgb: [1, 0, 0],
header: 'Hello!',
}
)
);
existingPage.addContentStreams(pdfDoc.register(newContentStream));
const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);
return pdfBytes;
};
Hello @ryanlozon!
I think your problem stems from the convertStringPdfToBinary function you're using. Try using the following:
const BASE64_MARKER = ';base64,';
const convertStringPdfToBinary = (pdfString) => {
const base64Index = pdfString.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
const base64 = pdfString.substring(base64Index);
return new Uint8Array(decode(base64));
};
Please let me know if this does the trick.
Hi @Hopding,
Thanks for responding so quickly. Unfortunately that didn't solve the problem. I'm still getting the same error: "PDF is missing a header".
Would some context help? I'm trying to modify fedex pdf labels.
@ryanlozon Can you please share a sample PDF file I can use to reproduce the issue? I'm afraid it will be difficult to troubleshoot further without one. If you could provide both the PDF file and the base64 string you are decoding, that would be helpful.
@Hopding my apologies, it turns out that there was no need to convert it base64. Thanks again for your help. This is a wonderful library.
const pdfDataToBuffer = Buffer.from(originalPdf.fileData, 'base64');
const pdfDoc = await PDFDocument.load(pdfDataToBuffer); const newPdfDoc = await PDFDocument.create();
i have a base64 string of pdf file saved in db, i call the file from db, then convert to buffer type, then tried to load it using pdf-lib. but its giving this error : "No PDF header found at MissingPDFHeaderError.PDFParsingError [as constructor] (C:\Users\adars\Documents\GitHub\pdf-editor-backend\node_modules\pdf-lib\cjs\core\errors.js:229:24) at new MissingPDFHeaderError (C:\Users\adars\Documents\GitHub\pdf-editor-backend\node_modules\pdf-lib\cjs\core\errors.js:306:24)"
i tried , the the base64 string i get from database is working , i checked it , can anyone help me with this