All files / amplify/utils transcript-pdf.ts

3.77% Statements 2/53
100% Branches 0/0
0% Functions 0/1
3.77% Lines 2/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671x     1x                                                                                                                              
import {jsPDF} from "jspdf";
import type {BaseTranscriptPart, Font, TranscriptComment} from "./types";
 
export function createPdf(
    transcriptParts: BaseTranscriptPart[],
    comments: TranscriptComment[],
    font?: Font,
): jsPDF {
    const pdf = new jsPDF();
    const pageWidth = pdf.internal.pageSize.getWidth() - 20;
    const pageHeight = pdf.internal.pageSize.getHeight() - 20;
 
    pdf.setFontSize(16);
    pdf.text("Transcript with comments", 10, 10);
 
    let fullTranscription = "";
    transcriptParts.forEach((part, partIndex) => {
        part.text.split(" ").forEach((word, textIndex) => {
            const comment = comments.find(comment =>
                comment.index === textIndex
                && comment.partIndex === partIndex
                && !comment.isTranslated
            );
            const text = comment ? `${word} [${comment.text}]` : word;
            fullTranscription += text + " ";
        });
 
        fullTranscription += "\n";
 
        part.translatedText.split(" ").forEach((word, translatedTextIndex) => {
            const comment = comments.find(comment =>
                comment.index === translatedTextIndex
                && comment.partIndex === partIndex
                && comment.isTranslated
            );
            const text = comment ? `${word} [${comment.text}]` : word;
            fullTranscription += text + " ";
        });
 
        fullTranscription += "\n\n";
    });
 
    if (font !== undefined) {
        const filename = `${font.name}.ttf`;
        pdf.addFileToVFS(filename, font.base64);
        pdf.addFont(filename, font.name, "normal");
        pdf.setFont(font.name);
    }
 
    pdf.setFontSize(12);
 
    const lines = pdf.splitTextToSize(fullTranscription.trim(), pageWidth) as string[];
 
    let y = 20;
 
    lines.forEach(line => {
        if (y + 10 > pageHeight) {
            pdf.addPage();
            y = 10;
        }
        pdf.text(line, 10, y);
        y += 10;
    });
 
    return pdf;
}