### Measuring Text Size (JavaScript) Source: https://github.com/hopding/react-native-pdf-lib/blob/master/README.md Shows how to use the measureText function to get the dimensions of a string based on a specified font name and size. This is useful for layout calculations like centering text. ```javascript return PDFLib.measureText( "My Centered Title", "Franklin Gothic Medium", 14 ).then(result => { console.log("The text size is: ", result); }); ``` -------------------------------- ### Creating a New PDF Document with react-native-pdf-lib (JavaScript) Source: https://github.com/hopding/react-native-pdf-lib/blob/master/README.md This snippet demonstrates how to create a new PDF document using `react-native-pdf-lib`. It shows how to create `PDFPage` objects, add text and shapes, include images from file paths, and then combine these pages into a new `PDFDocument` which is written to the device's documents directory. It requires importing `PDFLib`, `PDFDocument`, and `PDFPage`. ```JavaScript // Import from 'react-native-pdf-lib' import PDFLib, { PDFDocument, PDFPage } from 'react-native-pdf-lib'; // Create a PDF page with text and rectangles const page1 = PDFPage .create() .setMediaBox(200, 200) .drawText('You can add text and rectangles to the PDF!', { x: 5, y: 235, color: '#007386', }) .drawRectangle({ x: 25, y: 25, width: 150, height: 150, color: '#FF99CC', }) .drawRectangle({ x: 75, y: 75, width: 50, height: 50, color: '#99FFCC', }); // Create a PDF page with text and images const jpgPath = // Path to a JPG image on the file system... const pngPath = // Path to a PNG image on the file system... const page2 = PDFPage .create() .setMediaBox(250, 250) .drawText('You can add JPG images too!') .drawImage(jpgPath, 'jpg', { x: 5, y: 125, width: 200, height: 100, }) .drawImage(pngPath, 'png', { x: 5, y: 25, width: 200, height: 100, }); // Create a new PDF in your app's private Documents directory const docsDir = await PDFLib.getDocumentsDirectory(); const pdfPath = `${docsDir}/sample.pdf`; PDFDocument .create(pdfPath) .addPages(page1, page2) .write() // Returns a promise that resolves with the PDF's path .then(path => { console.log('PDF created at: ' + path); // Do stuff with your shiny new PDF! }); ``` -------------------------------- ### Configuring Custom Font Assets (rnpm) Source: https://github.com/hopding/react-native-pdf-lib/blob/master/README.md Configures the react-native packager (rnpm) to include the custom fonts directory in the app's assets, ensuring they are bundled correctly for use within the application. ```json "rnpm": { "assets": [ "./assets/fonts" ] } ``` -------------------------------- ### Modifying an Existing PDF Document with react-native-pdf-lib (JavaScript) Source: https://github.com/hopding/react-native-pdf-lib/blob/master/README.md This snippet illustrates how to modify an existing PDF file. It shows how to use `PDFPage.modify()` to target specific pages by index, add new elements like text and images, and also how to add entirely new pages using `PDFPage.create()`. The modified pages and new pages are then applied to the existing document using `PDFDocument.modify()` and `addPage()`, and the changes are written back to the file system. It requires importing `PDFLib`, `PDFDocument`, and `PDFPage`. ```JavaScript // Import from 'react-native-pdf-lib' import PDFLib, { PDFDocument, PDFPage } from 'react-native-pdf-lib'; // Modify first page in document const page1 = PDFPage .modify(0) .drawText('This is a modification on the first page!', { x: 5, y: 235, color: '#F62727', }) .drawRectangle({ x: 150, y: 150, width: 50, height: 50, color: '#81C744', }); // Modify second page in document const jpgPath = // in iOS Path to a JPG image on the file system... in Android path to the assert const pngPath = // in iOS Path to a PNG image on the file system... in Android path to the assert const page2 = PDFPage .modify(1) .drawText('You can add images to modified pages too!') .drawImage( jpgPath, 'jpg', { x: 5, y: 125, width: 200, height: 100, source: 'assets' // 'assets' to get image from Android assets 'path' to get image from imagePath } ) .drawImage( pngPath, 'png', { x: 5, y: 25, width: 200, height: 100, source: 'path' // 'assets' to get image from Android assets 'path' to get image from imagePath } ); // Create a PDF page to add to document const page3 = PDFPage .create() .setMediaBox(200, 200) .drawText('You can add new pages to a modified PDF as well!', { x: 5, y: 235, color: '#007386', }); const existingPDF = 'path/to/existing.pdf'; PDFDocument .modify(existingPDF) .modifyPages(page1, page2) .addPage(page3) .write() // Returns a promise that resolves with the PDF's path .then(path => { console.log('PDF modified at: ' + path); }); ``` -------------------------------- ### Drawing Text with Custom Font (JavaScript) Source: https://github.com/hopding/react-native-pdf-lib/blob/master/README.md Demonstrates how to create a PDF page and draw text using a previously linked custom font ('Franklin Gothic Medium'). It sets the text position, color, and specifies the font name. ```javascript const page1 = PDFPage.create() .setMediaBox(200, 200) .drawText("This text is using the font Franklin Gothic Medium!", { x: 5, y: 235, color: "#F62727", fontName: "Franklin Gothic Medium" }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.