-
-
Notifications
You must be signed in to change notification settings - Fork 706
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initialize PDFDocument.pageCount correctly * Lazily initialize PDFDocument.pageCount * Add unit tests for PDFDocument.getPageCount() * Fix page count caching bug
- Loading branch information
Showing
4 changed files
with
58 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,19 @@ | ||
import fs from 'fs'; | ||
import fetch from 'node-fetch'; | ||
import { openPdf, Reader } from './open'; | ||
|
||
import fontkit from '@pdf-lib/fontkit'; | ||
import { PDFDocument, rgb } from 'src/index'; | ||
import { PDFDocument } from 'src/index'; | ||
|
||
async function embedImages() { | ||
const url = 'https://pdf-lib.js.org/assets/ubuntu/Ubuntu-R.ttf'; | ||
const fontBytes = await fetch(url).then((res) => res.arrayBuffer()); | ||
(async () => { | ||
const pdfDoc = await PDFDocument.load( | ||
fs.readFileSync('assets/pdfs/normal.pdf'), | ||
); | ||
|
||
const pdfDoc = await PDFDocument.create(); | ||
|
||
pdfDoc.registerFontkit(fontkit); | ||
const customFont = await pdfDoc.embedFont(fontBytes); | ||
|
||
const page = pdfDoc.addPage(); | ||
|
||
const text = 'This is text in an embedded font!'; | ||
const textSize = 35; | ||
const textWidth = customFont.widthOfTextAtSize(text, textSize); | ||
const textHeight = customFont.heightAtSize(textSize); | ||
|
||
page.drawText(text, { | ||
x: 40, | ||
y: 450, | ||
size: textSize, | ||
font: customFont, | ||
color: rgb(0, 0.53, 0.71), | ||
}); | ||
page.drawRectangle({ | ||
x: 40, | ||
y: 450, | ||
width: textWidth, | ||
height: textHeight, | ||
borderColor: rgb(1, 0, 0), | ||
borderWidth: 1.5, | ||
}); | ||
console.log('Count:', pdfDoc.getPageCount()); | ||
pdfDoc.removePage(1); | ||
|
||
const pdfBytes = await pdfDoc.save(); | ||
|
||
fs.writeFileSync('./out.pdf', pdfBytes); | ||
|
||
openPdf('./out.pdf', Reader.Preview); | ||
} | ||
|
||
embedImages(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters