From PDF to Javascript
Resoning
Been able to modify the content or view, as not everything should be adjusted to a printer.
Process
Packages available
Package | Advantage on PDF |
---|---|
Mozilla pdfjs | Easy rendering |
pdf-lib | Easy extracting |
Ruksa pdfjs | Easy creation |
js-pdf | Complete but not as powerful |
Extracting from the old PDF and modify
Read local file with Mozilla pdflib
Make and file input button and then process the uploaded file using the
FileReader
web API.1
<input type="file" id="file-selector" accept=".pdf" onChange={onFileSelected} />
Filereader
API works with callbacks, butasync/await
can be used with as a helper.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20// helper function
function readFileAsync(file) {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
// selector to upload file
const onFileSelected = async (e) => {
const fileList = e.target.files;
if (fileList?.length > 0) {
// store it as an array buffer
const pdfArrayBuffer = await readFileAsync(fileList[0]);
}
};
Extract PDF file
Get an array with the page numbers of the PDF
1
2
3
4
5function range(start, end) {
let length = end - start + 1;
return Array.from({ length }, (_, i) => start + i - 1);
// add -1 at the end, as number in programming start in 0
}Extraction
1
2
3
4
5
6
7
8
9
10
11
12
13import { PDFDocument } from "pdf-lib";
// we pass the buffer we got on reading step as input
async function extractPdfPage(arrayBuff) {
const pdfSrcDoc = await PDFDocument.load(arrayBuff);
const pdfNewDoc = await PDFDocument.create();
// copy only the desired pages
const pages = await pdfNewDoc.copyPages(pdfSrcDoc,range(2,3));
pages.forEach(page=>pdfNewDoc.addPage(page));
const newpdf= await pdfNewDoc.save();
return newpdf;
}
// it returns an Uint8Array
Render the new PDF in the browser
Make a URL out of it and render it inside an
iframe
.1
2
3
4
5
6
7function renderPdf(uint8array) {
const tempblob = new Blob([uint8array], {
type: "application/pdf",
});
const docUrl = URL.createObjectURL(tempblob);
setPdfFileData(docUrl);
}You may use your custom PDF viewer using the pdfjs library as I mentioned above.
Download the new PDF
Use the download function from your browser.
Full code
1 | import { useState } from "react"; |