Skip to content

Instantly share code, notes, and snippets.

@skaag
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skaag/9928676 to your computer and use it in GitHub Desktop.
Save skaag/9928676 to your computer and use it in GitHub Desktop.
A sample CoffeeScript to test dynamic page height (useful for thermal printers where the length of the printout is unlimited).
PDFDocument = require './'
tiger = require 'tiger'
fs = require 'fs'
# Create a new PDFDocument
doc = new PDFDocument
margins: { top: 2, bottom: 2, left: 1, right: 1 }
size: [ 245, 690 ]
doc.pipe fs.createWriteStream('out.pdf')
# Set some meta data
doc.info['Title'] = 'Receipt'
doc.info['Author'] = 'Skaag'
# Register a font name for use later
doc.registerFont('DejaVuSans', 'fonts/DejaVuSans.ttf')
# Set the font, draw some text, and embed an image
doc.font('DejaVuSans')
.image('images/test.png', 2, 2, width: 240)
.fontSize(14)
.text('Thank you for printing me!', width: 245, align: 'center', bold: true )
.fontSize(10)
.text('Please keep this receipt for your record', width: 245, align: 'center', bold: false )
doc.lineWidth(2)
.roundedRect( 5, 110, 235, 300, 6 )
.roundedRect( 5, 420, 235, 120, 6 )
.roundedRect( 5, 550, 235, 120, 6 )
.stroke()
loremIpsum = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit. Ut nec accumsan nisl. Suspendisse rhoncus nisl posuere tortor tempus et dapibus elit porta. Cras leo neque, elementum a rhoncus ut, vestibulum non nibh. Phasellus pretium justo turpis. Etiam vulputate, odio vitae tincidunt ultricies, eros odio dapibus nisi, ut tincidunt lacus arcu eu elit. Aenean velit erat, vehicula eget lacinia ut, dignissim non tellus. Aliquam nec lacus mi, sed vestibulum nunc. Suspendisse potenti. Curabitur vitae sem turpis. Vestibulum sed neque eget dolor dapibus porttitor at sit amet sem. Fusce a turpis lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
Mauris at ante tellus. Vestibulum a metus lectus. Praesent tempor purus a lacus blandit eget gravida ante hendrerit. Cras et eros metus. Sed commodo malesuada eros, vitae interdum augue semper quis. Fusce id magna nunc. Curabitur sollicitudin placerat semper. Cras et mi neque, a dignissim risus. Nulla venenatis porta lacus, vel rhoncus lectus tempor vitae. Duis sagittis venenatis rutrum. Curabitur tempor massa tortor.
'''
# Draw some text wrapped to 412 points wide
doc.text('And here is some wrapped text...', 10, 120)
.font('Helvetica', 10)
.moveDown() # move down 1 line
.text(loremIpsum, width: 225, align: 'justify', indent: 30, paragraphGap: 5)
doc.page.dictionary.data.MediaBox[1] = doc.page.height - doc.y;
doc.page.height = doc.y;
doc.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment