UNPKG

@pdfme/converter

Version:

TypeScript base PDF generator and React base UI. Open source, developed by the community, and completely free to use under the MIT license!

38 lines 1.69 kB
import * as pdfjsLib from 'pdfjs-dist'; // @ts-expect-error - PDFJSWorker import is not properly typed but required for functionality import PDFJSWorker from 'pdfjs-dist/build/pdf.worker.entry.js'; import { pdf2img as _pdf2img } from './pdf2img.js'; import { pdf2size as _pdf2size } from './pdf2size.js'; pdfjsLib.GlobalWorkerOptions.workerSrc = PDFJSWorker; function dataURLToArrayBuffer(dataURL) { // Split out the actual base64 string from the data URL scheme const base64String = dataURL.split(',')[1]; // Decode the Base64 string to get the binary data const byteString = atob(base64String); // Create a typed array from the binary string const arrayBuffer = new ArrayBuffer(byteString.length); const uintArray = new Uint8Array(arrayBuffer); for (let i = 0; i < byteString.length; i++) { uintArray[i] = byteString.charCodeAt(i); } return arrayBuffer; } export const pdf2img = async (pdf, options = {}) => _pdf2img(pdf, options, { getDocument: (pdf) => pdfjsLib.getDocument(pdf).promise, createCanvas: (width, height) => { const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; return canvas; }, canvasToArrayBuffer: (canvas, imageType) => { // Using type assertion to handle the canvas method const dataUrl = canvas.toDataURL(`image/${imageType}`); return dataURLToArrayBuffer(dataUrl); }, }); export const pdf2size = async (pdf, options = {}) => _pdf2size(pdf, options, { getDocument: (pdf) => pdfjsLib.getDocument(pdf).promise, }); export { img2pdf } from './img2pdf.js'; //# sourceMappingURL=index.browser.js.map