File size: 1,239 Bytes
f44cf45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

// Use global jspdf from CDN
declare const jspdf: any;

export const createMangaPdf = (images: string[], title: string): void => {
    const { jsPDF } = jspdf;
    // Using a common manga/comic book aspect ratio (3:4) for PDF pages.
    const pdfWidth = 150; // mm
    const pdfHeight = 200; // mm to maintain 3:4 aspect ratio
    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`);
};