|
| 1 | +var PDFDocument = require('../'); |
| 2 | +var fs = require('fs'); |
| 3 | + |
| 4 | +// Create a new PDFDocument |
| 5 | +var doc = new PDFDocument({ |
| 6 | + autoFirstPage: true, |
| 7 | + bufferPages: true, |
| 8 | + pdfVersion: '1.5', |
| 9 | + // @ts-ignore PDF/UA needs to be enforced for PAC accessibility checker |
| 10 | + subset: 'PDF/UA', |
| 11 | + tagged: true, |
| 12 | + displayTitle: true, |
| 13 | + lang: 'en-US', |
| 14 | + fontSize: 12, |
| 15 | +}); |
| 16 | + |
| 17 | +doc.pipe(fs.createWriteStream('accessible-links.pdf')); |
| 18 | + |
| 19 | +// Set some meta data |
| 20 | +doc.info['Title'] = 'Test Document'; |
| 21 | +doc.info['Author'] = 'Devon Govett'; |
| 22 | + |
| 23 | +// Initialise document logical structure |
| 24 | +var struct = doc.struct('Document'); |
| 25 | +doc.addStructure(struct); |
| 26 | + |
| 27 | +// Register a font name for use later |
| 28 | +doc.registerFont('Palatino', 'fonts/PalatinoBold.ttf'); |
| 29 | + |
| 30 | +// Set the font and draw some text |
| 31 | +struct.add( |
| 32 | + doc.struct('P', () => { |
| 33 | + doc |
| 34 | + .font('Palatino') |
| 35 | + .fontSize(25) |
| 36 | + .text('Some text with an embedded font! ', 100, 100); |
| 37 | + }), |
| 38 | +); |
| 39 | + |
| 40 | +// Add another page |
| 41 | +doc.addPage(); |
| 42 | + |
| 43 | +// Add some text with annotations |
| 44 | +var linkSection = doc.struct('Sect'); |
| 45 | +struct.add(linkSection); |
| 46 | + |
| 47 | +var paragraph = doc.struct('P'); |
| 48 | +linkSection.add(paragraph); |
| 49 | + |
| 50 | +paragraph.add( |
| 51 | + doc.struct('Span', () => { |
| 52 | + doc |
| 53 | + .font('Palatino') |
| 54 | + .fillColor('black') |
| 55 | + .text('This is some text before ', 100, 100, { |
| 56 | + continued: true, |
| 57 | + }); |
| 58 | + }), |
| 59 | +); |
| 60 | + |
| 61 | +paragraph.add( |
| 62 | + doc.struct( |
| 63 | + 'Link', |
| 64 | + { |
| 65 | + alt: 'Here is a link! ', |
| 66 | + }, |
| 67 | + () => { |
| 68 | + doc.fillColor('blue').text('Here is a link!', { |
| 69 | + link: 'http://google.com/', |
| 70 | + underline: true, |
| 71 | + continued: true, |
| 72 | + }); |
| 73 | + }, |
| 74 | + ), |
| 75 | +); |
| 76 | + |
| 77 | +paragraph.add( |
| 78 | + doc.struct('Span', () => { |
| 79 | + doc.fillColor('black').text(' and this is text after the link.'); |
| 80 | + }), |
| 81 | +); |
| 82 | + |
| 83 | +paragraph.end(); |
| 84 | +linkSection.end(); |
| 85 | + |
| 86 | +// End and flush the document |
| 87 | +doc.end(); |
0 commit comments