|
|
|
|
|
|
|
|
declare const jspdf: any; |
|
|
|
|
|
export const createMangaPdf = (images: string[], title: string): void => { |
|
|
const { jsPDF } = jspdf; |
|
|
|
|
|
const pdfWidth = 150; |
|
|
const pdfHeight = 200; |
|
|
const doc = new jsPDF({ |
|
|
orientation: 'portrait', |
|
|
unit: 'mm', |
|
|
format: [pdfWidth, pdfHeight] |
|
|
}); |
|
|
|
|
|
images.forEach((imgData, index) => { |
|
|
if (index > 0) { |
|
|
doc.addPage(); |
|
|
} |
|
|
const imgProps = doc.getImageProperties(`data:image/jpeg;base64,${imgData}`); |
|
|
const aspectRatio = imgProps.width / imgProps.height; |
|
|
|
|
|
let imgWidth = pdfWidth; |
|
|
let imgHeight = pdfWidth / aspectRatio; |
|
|
|
|
|
if (imgHeight > pdfHeight) { |
|
|
imgHeight = pdfHeight; |
|
|
imgWidth = pdfHeight * aspectRatio; |
|
|
} |
|
|
|
|
|
const x = (pdfWidth - imgWidth) / 2; |
|
|
const y = (pdfHeight - imgHeight) / 2; |
|
|
|
|
|
doc.addImage(`data:image/jpeg;base64,${imgData}`, 'JPEG', x, y, imgWidth, imgHeight); |
|
|
}); |
|
|
|
|
|
const safeTitle = title.replace(/[^a-z0-9]/gi, '_').toLowerCase(); |
|
|
doc.save(`${safeTitle}_manga.pdf`); |
|
|
}; |
|
|
|