Web Development

PDFKit Measurement Unit Node.js

What is the measurement unit used in PDFKit (Node.js)? Quick answer: They use points (pt) as the measurement unit. 1 inch is equal to 72 points.

Sariful Islam

Sariful Islam

PDFKit Measurement Unit Node.js - Image | Sariful Islam

When working with PDFKit in Node.js, understanding measurement units is crucial for creating precisely positioned and sized PDF elements. This guide will explain PDFKit’s measurement system and provide you with a handy converter tool.

What Measurement Unit Does PDFKit Use?

PDFKit uses points (pt) as its default measurement unit.

  • 1 point = 1/72 of an inch
  • 1 inch = 72 points
  • 1 point ≈ 0.352778 millimeters
  • 1 mm = 2.83465 pt

This is the same measurement system used by most PDF specifications and desktop publishing software.

PDFKit Measurement Converter

Unit Converter

Common Conversions

Here are some common conversions you’ll need when working with PDFKit:

Points to Other Units

  • 72 pt = 1 inch
  • 28.35 pt ≈ 1 cm
  • 2.83 pt ≈ 1 mm

Standard Paper Sizes in Points

  • A4: 595 x 842 points
  • Letter: 612 x 792 points
  • Legal: 612 x 1008 points
  • A3: 842 x 1191 points

Practical Examples in PDFKit

Here are some practical examples of how to use measurements in PDFKit:

Setting Page Size

const PDFDocument = require('pdfkit');

// Create a new PDF document with A4 size
const doc = new PDFDocument({
  size: [595, 842] // A4 in points
});

// Or use predefined sizes
const doc2 = new PDFDocument({
  size: 'A4' // PDFKit handles the conversion
});

Positioning Elements

// Add text at specific coordinates (in points)
doc.text('Hello World', 72, 72); // 1 inch from left and top

// Draw a rectangle
doc.rect(72, 144, 144, 72) // x, y, width, height in points
   .stroke();

// Add margins (1 inch = 72 points)
const margin = 72;
doc.text('Content with margins', margin, margin, {
  width: doc.page.width - (margin * 2)
});

Working with Images

// Add an image with specific dimensions
doc.image('image.jpg', 72, 72, {
  width: 216, // 3 inches in points
  height: 144 // 2 inches in points
});

Tips for Working with PDFKit Measurements

  1. Always work in points when using PDFKit to avoid confusion
  2. Use the converter above to quickly convert from familiar units
  3. Remember common conversions: 72pt = 1in, 28.35pt ≈ 1cm
  4. Test your layouts with different page sizes to ensure consistency
  5. Use variables for common measurements to make your code more maintainable
const INCH = 72;
const CM = 28.35;
const MARGIN = INCH; // 1 inch margin

doc.text('Title', MARGIN, MARGIN);
doc.text('Content', MARGIN, MARGIN + (0.5 * INCH));

Common Pitfalls

  • Don’t mix units: Stick to points throughout your PDFKit code
  • Be aware of coordinate system: PDFKit uses top-left as origin (0,0)
  • Consider DPI: When converting from pixels, remember that PDFKit assumes 72 DPI by default
  • Page boundaries: Always check that your content fits within the page dimensions

Conclusion

Understanding PDFKit’s point-based measurement system is essential for creating professional PDF documents. Use the converter tool above to quickly translate between units, and remember that 72 points equal 1 inch. This knowledge will help you create precisely positioned and properly sized PDF content in your Node.js applications.

With this foundation, you can confidently work with PDFKit to create everything from simple reports to complex document layouts with pixel-perfect precision.