Skip to content

Commit

Permalink
⚡ Append newline to PDF output more efficiently
Browse files Browse the repository at this point in the history
This change uses a more efficient method to append the trailing newline
to the PDF output. Instead of creating a new array with the spread
operator, it creates a new Uint8Array with the correct length and copies
the original data into it.
  • Loading branch information
ralfstx committed Jan 8, 2025
1 parent 36bb196 commit 7cdd314
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/render/render-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export async function renderDocument(def: DocumentDefinition, pages: Page[]): Pr
PDFHexString.of(fileId.toUpperCase()),
]);
const data = await pdfDoc.save();
// add trailing newline
return new Uint8Array([...data, 10]);
return appendNewline(data);
}

function setMetadata(doc: PDFDocument, info?: Metadata) {
Expand Down Expand Up @@ -74,3 +73,10 @@ async function sha256Hex(input: string): Promise<string> {
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
}

function appendNewline(data: Uint8Array): Uint8Array {
const result = new Uint8Array(data.length + 1);
result.set(data, 0);
result[data.length] = 10;
return result;
}

0 comments on commit 7cdd314

Please sign in to comment.