Skip to content

Instantly share code, notes, and snippets.

@gettalong
Last active July 22, 2016 21:36
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 gettalong/c24c9302945d095841d5a3e320ae62c7 to your computer and use it in GitHub Desktop.
Save gettalong/c24c9302945d095841d5a3e320ae62c7 to your computer and use it in GitHub Desktop.
HexaPDF examples showing off the standard 14 PDF fonts
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
require 'hexapdf/document'
doc = HexaPDF::Document.new
HexaPDF::FontLoader::Standard14::MAPPING.each do |font_name, mapping|
mapping.each_key do |variant|
canvas = doc.pages.add_page.canvas
canvas.font("Helvetica", size: 14)
canvas.show_text("#{font_name} #{variant != :none ? variant : nil}", at: [100, 700])
canvas.font(font_name, size: 14, variant: variant)
font = canvas.font
encoding = font.font.encoding
fragment = canvas.text_fragment
(0..15).each do |y|
fragment.data.clear
fragment.offset = [100, 650 - y * 20]
(0..15).each do |x|
code = y * 16 + x
glyph = font.glyph(encoding.name(code)) rescue font.glyph(:space)
fragment.data << glyph << -(2000 - glyph.width)
end
canvas.begin_text.show_glyphs(fragment).end_text
end
end
end
doc.task(:optimize, object_streams: :generate)
doc.write("standard_pdf_fonts.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment