Skip to content

Instantly share code, notes, and snippets.

@devongovett
Created July 10, 2011 16:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save devongovett/1074661 to your computer and use it in GitHub Desktop.
Save devongovett/1074661 to your computer and use it in GitHub Desktop.
PDFKit examples
doc.addPage
size: 'legal'
layout: 'landscape'
doc.circle(100, 100, 50)
.lineWidth(3)
.fillOpacity(0.8)
.fillAndStroke("red", "#900")
PDFDocument = require 'pdfkit'
doc = new PDFDocument
doc.circle(100, 100, 50)
.dash(5, space: 10)
.stroke()
# these examples are easier to see with a large line width
doc.lineWidth(25)
# line cap settings
doc.lineCap('butt')
.moveTo(50, 50)
.lineTo(100, 50)
.stroke()
doc.lineCap('round')
.moveTo(150, 50)
.lineTo(200, 50)
.stroke()
# square line cap shown with a circle instead of a line so you can see it
doc.lineCap('square')
.moveTo(250, 50)
.circle(275, 60, 15)
.stroke()
# line join settings
doc.lineJoin('miter')
.rect(50, 150, 50, 50)
.stroke()
doc.lineJoin('round')
.rect(150, 150, 50, 50)
.stroke()
doc.lineJoin('bevel')
.rect(250, 150, 50, 50)
.stroke()
# Set some meta data on an existing document
doc.info['Title'] = 'Test Document'
doc.info['Author'] = 'Devon Govett'
# Create a new document with some metadata
doc = new PDFDocument
info:
Title: 'Test Document'
Author: 'Devon Govett'
doc.moveTo(100, 20) # set the current point
.lineTo(200, 160) # draw a line
.quadraticCurveTo(230, 200, 250, 120) # draw a quadratic curve
.bezierCurveTo(290, -40, 300, 200, 400, 150) # draw a bezier curve
.lineTo(500, 90) # draw another line
.stroke() # stroke the path
doc.polygon [100, 100], [50, 200], [150, 200]
doc.stroke()
doc.path('M 100,20 L 200,160 Q 230,00 250,120 C 290,-40 300,200 400,150 L 500,90')
.stroke()
# Save the document as a file
doc.write 'out.pdf'
# Get a string representation of the document
doc.output (string) ->
console.log string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment