11import { readFile , writeFile } from 'node:fs/promises' ;
22
3- import { makePdf } from 'pdfmkr' ;
4-
5- const fontData = await readFile ( './fonts/DejaVuSansCondensed.ttf' ) ;
6- const fontDataBold = await readFile ( './fonts/DejaVuSansCondensed-Bold.ttf' ) ;
7- const fontDataOblique = await readFile ( './fonts/DejaVuSansCondensed-Oblique.ttf' ) ;
3+ import { columns , PdfMaker , rows , text } from 'pdfmkr' ;
84
95const loremIpsum =
106 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor' +
@@ -15,61 +11,54 @@ const loremIpsum =
1511 'mollit anim id est laborum.' ;
1612
1713const def = {
18- fonts : {
19- 'DejaVu-Sans' : [
20- { data : fontData } ,
21- { data : fontDataOblique , italic : true } ,
22- { data : fontDataBold , bold : true } ,
23- ] ,
24- } ,
2514 margin : { x : '20mm' , y : '0.5cm' } ,
2615 defaultStyle : {
2716 fontSize : 12 ,
2817 } ,
29- header : {
30- columns : [ { text : 'PDF Maker' } , { text : 'Anchors' , textAlign : 'right' , width : 'auto' } ] ,
18+ header : columns ( [ text ( 'PDF Maker' ) , text ( 'Anchors' , { textAlign : 'right' , width : 'auto' } ) ] , {
3119 margin : { x : '20mm' , top : '1cm' } ,
32- } ,
33- footer : ( { pageNumber, pageCount } ) => ( {
34- text : `${ pageNumber } /${ pageCount ?? 0 } ` ,
35- textAlign : 'right' ,
36- margin : { x : '20mm' , bottom : '1cm' } ,
3720 } ) ,
21+ footer : ( { pageNumber, pageCount } ) =>
22+ text ( `${ pageNumber } /${ pageCount ?? 0 } ` , {
23+ textAlign : 'right' ,
24+ margin : { x : '20mm' , bottom : '1cm' } ,
25+ } ) ,
3826 content : [
39- {
40- rows : [
41- {
42- text : 'Contents' ,
27+ rows (
28+ [
29+ text ( 'Contents' , {
4330 fontSize : 18 ,
4431 margin : { bottom : 5 } ,
45- } ,
46- ...range ( 10 ) . map ( ( n ) => ( {
47- text : `Paragraph ${ n + 1 } ` ,
48- // Link to the paragraph with given id
49- link : `#par:${ n + 1 } ` ,
50- } ) ) ,
32+ } ) ,
33+ ...range ( 10 ) . map ( ( n ) =>
34+ text ( `Paragraph ${ n + 1 } ` , {
35+ // Link to the paragraph with given id
36+ link : `#par:${ n + 1 } ` ,
37+ } ) ,
38+ ) ,
5139 ] ,
52- margin : { bottom : 10 } ,
53- } ,
54- ...range ( 10 ) . map ( ( n ) => ( {
55- rows : [
56- {
57- text : `Paragraph ${ n + 1 } ` ,
58- breakAfter : 'avoid' ,
59- } ,
60- { text : loremIpsum } ,
61- ] ,
62- // Create an anchor with given id
63- id : `par:${ n + 1 } ` ,
64- margin : { top : 5 } ,
65- fontSize : 10 ,
66- } ) ) ,
40+ { margin : { bottom : 10 } } ,
41+ ) ,
42+ ...range ( 10 ) . map ( ( n ) =>
43+ rows ( [ text ( `Paragraph ${ n + 1 } ` , { breakAfter : 'avoid' } ) , text ( loremIpsum ) ] , {
44+ // Create an anchor with given id
45+ id : `par:${ n + 1 } ` ,
46+ margin : { top : 5 } ,
47+ fontSize : 10 ,
48+ } ) ,
49+ ) ,
6750 ] ,
6851} ;
6952
7053function range ( n ) {
7154 return [ ...Array ( n ) . keys ( ) ] ;
7255}
7356
74- const pdf = await makePdf ( def ) ;
75- writeFile ( './out/anchors.pdf' , pdf ) ;
57+ const pdfMaker = new PdfMaker ( ) ;
58+
59+ pdfMaker . registerFont ( await readFile ( './fonts/DejaVuSansCondensed.ttf' ) ) ;
60+ pdfMaker . registerFont ( await readFile ( './fonts/DejaVuSansCondensed-Bold.ttf' ) ) ;
61+ pdfMaker . registerFont ( await readFile ( './fonts/DejaVuSansCondensed-Oblique.ttf' ) ) ;
62+
63+ const pdf = await pdfMaker . makePdf ( def ) ;
64+ await writeFile ( './out/anchors.pdf' , pdf ) ;
0 commit comments