Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/modules/pdf-forge/store/usePdfStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,24 @@ async function processClientSide(task: PdfTask, onProgress: (p: number) => void)
}
case 'from-image': {
const doc = await PDFDocument.create();
for (let i = 0; i < files.length; i++) {
const imgBytes = await files[i].arrayBuffer();
const isJpg = files[i].type === 'image/jpeg';
const img = isJpg ? await doc.embedJpg(imgBytes) : await doc.embedPng(imgBytes);
let completed = 0;

const embeddedImages = await Promise.all(
files.map(async (file) => {
const imgBytes = await file.arrayBuffer();
const isJpg = file.type === 'image/jpeg';
const img = isJpg ? await doc.embedJpg(imgBytes) : await doc.embedPng(imgBytes);

completed++;
onProgress(10 + (80 * completed) / files.length);

return img;
})
);

for (const img of embeddedImages) {
const page = doc.addPage([img.width, img.height]);
page.drawImage(img, { x: 0, y: 0, width: img.width, height: img.height });
onProgress(10 + (80 * (i + 1)) / files.length);
}
return toBlob(await doc.save());
}
Expand Down