Created
July 10, 2011 20:55
-
-
Save devongovett/1074963 to your computer and use it in GitHub Desktop.
PDFKit Examples part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed pulvinar diam eu ' + | |
'dolor bibendum et varius diam laoreet. Morbi eget rutrum enim. Sed enim ipsum, ' + | |
'posuere nec hendrerit non, commodo quis tortor. Fusce id nisl augue. Fusce at ' + | |
'lectus ut libero vehicula imperdiet.' | |
doc.text 'This text is left aligned. ' + lorem, 100, 100, | |
width: 410 | |
align: 'left' | |
doc.moveDown() | |
doc.text 'This text is centered. ' + lorem, | |
width: 410 | |
align: 'center' | |
doc.moveDown() | |
doc.text 'This text is right aligned. ' + lorem, | |
width: 410 | |
align: 'right' | |
doc.moveDown() | |
doc.text 'This text is justified. ' + lorem, | |
width: 410 | |
align: 'justify' | |
# draw bounding rectangle | |
doc.rect(100, 100, 410, doc.y - 100) | |
.stroke() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add the link text | |
doc.fontSize(25) | |
.fillColor('blue') | |
.text('This is a link!', 100, 100) | |
# Measure the text | |
width = doc.widthOfString('This is a link!') | |
height = doc.currentLineHeight() | |
# Add the underline and link annotations | |
doc.underline(100, 100, width, height, color: 'blue') | |
.link(100, 100, width, height, 'http://google.com/') | |
# Create the highlighted text | |
doc.moveDown() | |
.fillColor('black') | |
.highlight(100, doc.y, doc.widthOfString('This text is highlighted!'), height) | |
.text('This text is highlighted!') | |
# Create the crossed out text | |
doc.moveDown() | |
.strike(100, doc.y, doc.widthOfString('STRIKE!'), height) | |
.text('STRIKE!') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a clipping path | |
doc.circle(100, 100, 100) | |
.clip() | |
# Draw a checkerboard pattern | |
for row in [0...10] | |
for col in [0...10] | |
color = if (col % 2) - (row % 2) then '#eee' else '#4183C4' | |
doc.rect(row * 20, col * 20, 20, 20) | |
.fill(color) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set the font size | |
doc.fontSize(25) | |
# Using a standard PDF font | |
doc.font('Times-Roman') | |
.text('Hello from Times Roman!', 100, 100) | |
.moveDown(0.5) | |
# Using a TrueType font (.ttf) | |
doc.font('fonts/GoodDog.ttf') | |
.text('This is Good Dog!') | |
.moveDown(0.5) | |
# Using a collection font (.ttc or .dfont) | |
doc.font('fonts/Chalkboard.ttc', 'Chalkboard-Bold') | |
.text('This is not Comic Sans.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Render the image at full size | |
doc.image('images/test.jpeg', 100, 100) | |
.text('Full size', 100, 85) | |
# Fit the image within the dimensions | |
doc.image('images/test.jpeg', 350, 100, fit: [100, 100]) | |
.rect(350, 100, 100, 100) | |
.stroke() | |
.text('Fit', 350, 85) | |
# Stretch the image | |
doc.image('images/test.jpeg', 350, 265, width: 200, height: 100) | |
.text('Stretch', 350, 250) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Register a font | |
doc.registerFont('Heading Font', 'fonts/Chalkboard.ttc', 'Chalkboard-Bold') | |
# Use the font later | |
doc.font('Heading Font') | |
.text('This is a heading.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doc.rotate(20, origin: [150, 150]) | |
.rect(100, 100, 100, 100) | |
.fill('gray') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doc.text 'Hello world!' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doc.text 'Hello world!', 100, 100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Initial setup | |
doc.fillColor('red') | |
.scale(0.8) | |
# Draw the path with the non-zero winding rule | |
doc.path('M 250,75 L 323,301 131,161 369,161 177,301 z') | |
.fill('non-zero') | |
# Draw the path with the even-odd winding rule | |
doc.translate(280, 0) | |
.path('M 250,75 L 323,301 131,161 369,161 177,301 z') | |
.fill('even-odd') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment