# This is the main manual for PDF::Writer. It is interpreted by PDF::TechBook # which implements an interpreter for the simple markup language presented # here. # # 0. Comments, headings, and directives must begin in column 0 and consume # the whole line. # 1. Comments begin with '#' at the beginning of the line. # 2. Directives are in the form '.[ [] ]', where # both args (arguments) and comment are optional. The arguments must be # separated from the directive by a space and comments must be separated # from the arguments by a space. The format of the arguments is directive # dependent. Directive names must begin with a letter, may only contain # the word characters. # 2.1. .newpage starts a new page. # 2.2. .pre starts a "preserved newlines" block of text. This is similar to # normal text (see #4) but the lines are not flowed. This is useful for # creating lists or other text that must be in a particular form # without switching to a code font. .pre blocks may not be nested in # any other block (that is to say, when an .endpre is encountered, the # text state is set to normal text). # 2.3. .endpre ends a preserved newlines block of text. # 2.4. .code starts a "code" block of text. Similar to .pre, this changes # the display font to the "code" font for the text, which is generally # a Courier font. .code blocks may be nested in .pre blocks (when an # .endcode is encountered, the text state is restored to the previous # text state, either normal or preserved newlines). # 2.5. .endcode ends a code block of text. # 2.6. .eval starts an eval block. Text between this line and .endeval will # be collected and passed to Ruby's Kernel#eval at $SAFE level 3. .eval # blocks may be nested in .pre or .code blocks. # 2.7. .endeval ends an eval block. # 2.8. .done stops processing on the document. # 2.9. .columns turns on and off column output. The number of columns is # specified as an argment, e.g., ".columns 3". Turn columns off with # ".columns 1" or ".columns 0". A second parameter may be provided # specifying the gutter width (e.g., ".columns 3 15"). # 2.10. .toc indicates that the table of contents should be generated with # the argument value as the title of the table of contents page. This # is an advisory value only. If this is not present, a table of # contents will not be automatically generated. # 2.11. .author, .title, .subject, and .keywords set values in the PDF info # object. The arguments are used as the values. # 2.12. .blist starts a list block. Lists may be nested within each other, # and will successively shift the left margin by 10% of the initial # left margin value. Each line in the list block will be treated as a # separate list item with a bullet inserted in front. This will be # inserted as a callback (e.g., or ) before the # text of the list item. # 2.13. .endblist will end the current list block. # 3. Headings are added to the text with the form '#' where # # is the level of the heading. The only heading levels currently # supported are 1 - 5. Heading levels 1 and 2 will automatically be added # to the table of contents. Headings *may* include markup (see #5), but # should not exceed a single line (the heading box is not currently set # up for this). All heading levels may also have an optional name # comprised word characters (a-zA-Z0-9_) for automated cross-reference # building. The form will be like this: '1name'. If the name is # not provided, the cross-reference identifier will be the position of # the identifier. # 3.1. 1 is a first-level heading with text of "chapter". Usually # used for chapter headings. # 3.2. 2
is a second-level heading with text of "section". Usually # used for section headings. # 3.3. 3
, 4
, and 5
, are headings that use the # text of "section" and identify various subsections of the text. # 3.4. Automated cross-reference handling is achived with the replacing text # directive 'xref': . The name # will be the name associated with the heading level and labeltype will # be either 'page', 'title', or 'text'. The cross-reference directive # is replaced with either the page number or the section label. The # page type can only be replaced if the cross-reference target has # already been placed on a document page. # 4. Normal text is treated as flowed text. That is, two lines back to back # will be joined into a single line and then wrapped by the text # formatting within PDF::Writer. Paragraphs are separated by an empty # line, a valid directive, or a heading. # 5. Within normal, preserved, code text, or headings, HTML-like markup may # be used for bold (), italic (), superscript (), # and subscript (). # Other markup options may be used with the callback tag. Clickable # external hyperlinks may be added by using the 'alink' callback: # LABEL # 6. Special characters (e.g., <, &, and >) must be escaped in the HTML # style. # # Please note that this syntax will be evolving in future versions of # PDF::Writer. # # Austin Ziegler # Tuesday, 3 May 2005 #-- # We want a table of contents for this document. .toc Table of Contents # Set the PDF document info. .author Austin Ziegler .title PDF::Writer for Ruby Manual .subject The use and care of PDF::Writer for Ruby. # Use .eval to do encoding configuration. .eval {{{ pdf.techbook_encoding = { :encoding => "WinAnsiEncoding", :differences => { 169 => "copyright", 215 => "multiply", } } .endeval }}} # Use .eval to create the heading and footing. .eval {{{ pdf.select_font "Helvetica", pdf.techbook_encoding # Put a heading on all pages. pdf.open_object do |heading| pdf.save_state pdf.stroke_color! Color::RGB::Black pdf.stroke_style! PDF::Writer::StrokeStyle::DEFAULT s = 6 t = "PDF::Writer for Ruby ~ Manual" w = pdf.text_width(t, s) / 2.0 x = pdf.margin_x_middle y = pdf.absolute_top_margin pdf.add_text(x - w, y, t, s) x = pdf.absolute_left_margin w = pdf.absolute_right_margin y -= (pdf.font_height(s) * 1.01) pdf.line(x, y, w, y).stroke pdf.restore_state pdf.close_object pdf.add_object(heading, :all_following_pages) end # Put a footing on all pages. pdf.open_object do |footing| pdf.save_state pdf.stroke_color! Color::RGB::Black pdf.stroke_style! PDF::Writer::StrokeStyle::DEFAULT s = 6 t = "http://rubyforge.org/projects/ruby-pdf" x = pdf.absolute_left_margin y = pdf.absolute_bottom_margin pdf.add_text(x, y, t, s) x = pdf.absolute_left_margin w = pdf.absolute_right_margin y += (pdf.font_height(s) * 1.05) pdf.line(x, y, w, y).stroke pdf.restore_state pdf.close_object pdf.add_object(footing, :all_following_pages) end .endeval }}} # Use .eval to create the title page and start page numbering. .eval {{{ # Get the info we need for placing page numbering after we draw the title # page. pny = pdf.absolute_bottom_margin pnx = pdf.absolute_right_margin pdf.margins_pt(54) # 54 point (3/4") margins on all sides... pdf.left_margin = 72 # ...except the left margin (1") image = File.join(pdf.techbook_source_dir, "images", "bluesmoke.jpg") pdf.add_image_from_file(image, 300, 50) # Draw the title page. title = "PDF::Writer for Ruby" size = 72 fh = pdf.font_height(size) * 1.01 fd = pdf.font_descender(size) * 1.01 pdf.save_state pdf.fill_color Color::RGB::Red pdf.stroke_color Color::RGB::Red pdf.rectangle(pdf.absolute_left_margin, 0, fh, pdf.page_height).fill pdf.fill_color Color::RGB::White pdf.stroke_color Color::RGB::White pdf.add_text(pdf.absolute_left_margin + fh + fd, 70, title, size, 90) pdf.restore_state pdf.select_font pdf.techbook_textfont, pdf.techbook_encoding pdf.text("\nNative Ruby PDF Document Creation\n", :font_size => 24, :justification => :right) info = <<-"INFO" The Ruby PDF Project http://rubyforge.org/projects/ruby-pdf version #{PDF::Writer::VERSION} INFO pdf.text(info, :font_size => 20, :justification => :right) info = <<-INFO Copyright © 2003–2005 Austin Ziegler INFO pdf.text(info, :font_size => 18, :justification => :right) pdf.open_here("Fit") pdf.techbook_fontsize = 12 .endeval }}} .newpage .eval {{{ pnx = pdf.absolute_right_margin pdf.start_page_numbering(pnx, 36, 6, :right, "", 1) .endeval }}} # The main document. 1Introduction PDF::Writer is designed to provide a pure Ruby way to dynamically create PDF documents. Obviously, this will not be as fast as one that uses a compiled extension, but it is surprisingly fast. This manual is, in some ways, a worst case scenario because of the number of examples that must be displayed. PDF::Writer does not yet implement the full PDF specification (any version), but it implements a substantial portion of it, with more to come. It also implements a more complex document layer on top of the portion that is implemented. This manual (manual.pdf) is generated by PDF::TechBook using the application runner (bin/techbook) and the text version of the manual (manual.pwd). It is a comprehesive introduction to the use of PDF::Writer and a simple markup language interpreter. PDF::Writer itself only implements a few markup items, mostly relating to bold and italic text. PDF::Writer is based on Adobe’s PDF Reference, Fifth Edition, version 1.6. This and earlier editions are available from the Adobe website. The original implementation was a port of the public domain R&OS PDF Class for PHP. Other demo programs are available in the demo/ directory. Alternatively, they may be downloaded separately from the Ruby PDF Tools project on RubyForge. 2 If this manual was generated from a local installation of PDF::Writer, then congratulations! PDF::Writer is installed. Otherwise, you are reading a manual generated otherwise. If you want to install PDF::Writer, you can download it from the Ruby PDF Tools project on RubyForge or install it with RubyGems. PDF::Writer has dependencies on Transaction::Simple 1.3.0 or later and color-tools 1.0.0 or later. These must be installed for PDF::Writer to work. RubyGems installation will automatically detect and offer to download and install these dependencies. PDF::Writer is installed with: .code {{{ % ruby setup.rb .endcode }}} .newpage 1MakingDocuments Document creation with PDF::Writer is quite easy. The following code will create a single-page document that contains the phrase “Hello, Ruby.” centered at the top of the page in 72-point (1”) type. .code {{{ # This code is demo/hello.rb require "pdf/writer" pdf = PDF::Writer.new pdf.select_font "Times-Roman" pdf.text "Hello, Ruby.", :font_size => 72, :justification => :center File.open("hello.pdf", "wb") { |f| f.write pdf.render } .endcode }}} This one, on the other hand, uses a very (in)famous phrase and image. Note that the images are JPEG and PNG—PDF::Writer cannot use GIF images. .code {{{ # This code is demo/chunkybacon.rb require "pdf/writer" pdf = PDF::Writer.new pdf.select_font "Times-Roman" pdf.text "Chunky Bacon!!", :font_size => 72, :justification => :center # PDF::Writer#image returns the image object that was added. i0 = pdf.image "../images/chunkybacon.jpg", :resize => 0.75 pdf.image "../images/chunkybacon.png", :justification => :center, :resize => 0.75 # It can reinsert an image if wanted. pdf.image i0, :justification => :right, :resize => 0.75 pdf.text "Chunky Bacon!!", :font_size => 72, :justification => :center File.open("chunkybacon.pdf", "wb") { |f| f.write pdf.render } .endcode }}} If those aren’t enough to whet your appetite for how easy PDF::Writer can be, well, no chunky bacon for you! .newpage 1FundamentalConcepts This section covers fundamental concepts to the creation of PDF documents that will assist in the understanding of the details that follow in later sections. 2CoordinateSpace PDF documents don’t use inches or millimetres for measurement, and the coordinate space is slightly different than that used by other graphics canvases. 3 PDF user units are by default points. A modern point is exactly 1/72” (1/72 inch); it is generally accepted that 72 point type is one inch in height. Historically, one point was 0.0138”, or just under 1/72”. .eval {{{ pdf.select_font("Helvetica") PDF::SimpleTable.new do |tab| tab.title = "PDF User Unit Conversions" tab.column_order.push(*%w(from1 to1 from2 to2)) tab.columns["from1"] = PDF::SimpleTable::Column.new("from1") { |col| col.heading = "From" } tab.columns["to1"] = PDF::SimpleTable::Column.new("to1") { |col| col.heading = "To" } tab.columns["from2"] = PDF::SimpleTable::Column.new("from2") { |col| col.heading = "From" } tab.columns["to2"] = PDF::SimpleTable::Column.new("to2") { |col| col.heading = "To" } tab.show_lines = :all tab.show_headings = true tab.orientation = :center tab.position = :center data = [ { "from1" => "1 point", "to1" => "0.3528 mm", "from2" => "1 point", "to2" => "1/72”" }, { "from1" => "10 mm", "to1" => "28.35 pts", "from2" => "", "to2" => "" }, { "from1" => "A4", "to1" => "210 mm × 297 mm", "from2" => "A4", "to2" => "595.28 pts × 841.89 pts" }, { "from1" => "LETTER", "to1" => "8½” × 11”", "from2" => "LETTER", "to2" => "612 pts × 792 pts" }, ] tab.data.replace data tab.render_on(pdf) end .endeval }}} It is possible to scale the user coordinates with PDF::Writer#scale_axis. With scale_axis(0.5, 0.5), each coordinate is ½ point in size (1/144”) in both the x and y directions. See the following discussion on PDF Coordinate Space for the limitation of performing transformations on the coordinate axis. PDF::Writer provides utility methods to convert between normal measurement units and and points. These utility methods are available either as class mehods or as instance methods. .eval {{{ $rm = pdf.right_margin pdf.right_margin += 175 y = pdf.absolute_bottom_margin + 40 pdf.stroke_style(PDF::Writer::StrokeStyle.new(1)) pdf.rectangle(400, y, 150, 200).stroke x0y0 = "(0, 0)" hh = pdf.font_height(9) + pdf.font_descender(9) pdf.add_text(405, y + hh - 2, x0y0, 9) x1y1 = "(150, 200)" ww = pdf.text_width(x1y1, 9) pdf.add_text(545 - ww, y + 198 - hh, x1y1, 9) .endeval }}} 4 Converts from centimetres to points. 4 Converts from inches to points. 4 Converts from millimetres to points. 3PDFCoordinateSpace The standard PDF coordinate space is a little different than might be expected. Most graphics canvases place the base coordinate (0, 0) in the upper left-hand corner of the canvas; the common PDF coordinate sytem places the base coordinate in the lower left-hand corner of the canvas. .eval {{{ pdf.right_margin = $rm .endeval }}} PDF::Writer uses the standard coordinate space. Any transformations on the coordinate space (or, more accurately, on the coordinate axis) may affect the usability of PDF::Writer. Standard transformations available against the coordinate space axis are rotations (counter-clockwise), scaling, translation (repositioning of the base coordinate) and skew. Each of these will be discussed in more detail in a later section of this manual, after other concepts have been introduced. NOTE: As of PDF::Writer 1.1.0, angle rotations are now counter-clockwise, not clockwise as in earlier versions. This is a necessary incompatible change to make transformations more compatible with other vector graphics systems. 2FontBasics All PDF readers support fourteen standard fonts; twelve of these fonts are bold, italic (or oblique), and bold-italic (bold-oblique) variants of three base font families and the other two are symbolic fonts. .eval {{{ pdf.select_font("Helvetica") PDF::SimpleTable.new do |tab| tab.title = "Default Fonts in PDF" tab.column_order.push(*%w(family name filename)) tab.columns["family"] = PDF::SimpleTable::Column.new("family") { |col| col.heading = "Family" } tab.columns["name"] = PDF::SimpleTable::Column.new("name") { |col| col.heading = "Name" } tab.columns["filename"] = PDF::SimpleTable::Column.new("filename") { |col| col.heading = "Filename" } tab.show_lines = :outer tab.show_headings = true tab.shade_headings = true tab.orientation = :center tab.position = :center data = [ { "family" => "Courier", "name" => "Courier", "filename" => "Courier.afm" }, { "family" => "", "name" => "Courier-Bold", "filename" => "Courier-Bold.afm" }, { "family" => "", "name" => "Courier-Oblique", "filename" => "Courier-Oblique.afm" }, { "family" => "", "name" => "Courier-BoldOblique", "filename" => "Courier-BoldOblique.afm" }, { "family" => "Helvetica", "name" => "Helvetica", "filename" => "Helvetica.afm" }, { "family" => "", "name" => "Helvetica-Bold", "filename" => "Helvetica-Bold.afm" }, { "family" => "", "name" => "Helvetica-Oblique", "filename" => "Helvetica-Oblique.afm" }, { "family" => "", "name" => "Helvetica-BoldOblique", "filename" => "Helvetica-BoldOblique.afm" }, { "family" => "Symbol", "name" => "Symbol", "filename" => "Symbol.afm" }, { "family" => "Times-Roman", "name" => "Times-Roman", "filename" => "Times-Roman.afm" }, { "family" => "", "name" => "Times-Bold", "filename" => "Times-Bold.afm" }, { "family" => "", "name" => "Times-Italic", "filename" => "Times-Italic.afm" }, { "family" => "", "name" => "Times-BoldItalic", "filename" => "Times-BoldItalic.afm" }, { "family" => "ZapfDingbats", "name" => "ZapfDingbats", "filename" => "ZapfDingbats.afm" }, ] tab.data.replace data tab.render_on(pdf) end .endeval }}} In addition to these fourteen standard fonts, PDF supports the embedding of PostScript Type 1, TrueType, and OpenType fonts. PDF::Writer explicitly supports Type 1 and TrueType fonts. Inasmuch as OpenType fonts may be compatible with TrueType fonts, they may be supported. Embedded fonts require font metrics information. See “”, “” and “” below for more information. PDF::Writer will find (or attempt to find) system font location(s) so that any existing Type 1 or TrueType fonts can be loaded directly from the system locations. These paths are found in PDF::Writer::FONT_PATH. By modifying this value, it is possible to tell PDF::Writer to find font files in alternate locations. Adobe font metrics (AFM) files are found in the paths described by PDF::Writer::FontMetrics::METRICS_PATH, the current directory, and the Ruby $LOAD_PATH. Fonts are selected with PDF::Writer#select_font. This will load the appropriate font metrics file and—if the font is not one of the fourteen built-in fonts—search for the associated Type 1 or TrueType font to embed in the generated PDF document. 3 This selects, and possibly loads, a font to be used from this point in the document. The font name is as noted above. The format and the use of the encoding parameter is discussed extensively in “”. The encoding directive will be effective only when the font is first loaded. .code {{{ # use a Times-Roman font with MacExpertEncoding pdf.select_font("Times-Roman", "MacExpertEncoding") # this next line should be equivalent pdf.select_font("Times-Roman", { :encoding => "MacExpertEncoding" }) # Set up the Helvetica font for use with German characters as an offset # of the WinAnsiEncoding. diff= { 196 => "Adieresis", 228 => "adieresis", 214 => "Odieresis", 246 => "odieresis", 220 => "Udieresis", 252 => "udieresis", 223 => "germandbls" } pdf.select_font("Helvetica", { :encoding => "WinAnsiEncoding", :differences => diff }) .endcode }}} 3FontFamilies It is possible to define font families in PDF::Writer so that text state translations may be performed. The only text state translations that PDF::Writer currently recognises are ‘b’ (bold), ‘i’ (italic), ‘bi’ (bold italic), and ‘ib’ (italic bold). 4 PDF::Writer#font_families is a Hash that maps the default font name (such as “Helvetica”) to a mapping of text states and font names, as illustrated below. .code {{{ pdf = PDF::Writer.new # Note: "bi" (<b><i>) can be implemented as a # different font than "ib" (<i><b>). pdf.font_families["Helvetica"] = { "b" => "Helvetica-Bold", "i" => "Helvetica-Oblique", "bi" => "Helvetica-BoldOblique", "ib" => "Helvetica-BoldOblique" } .endcode }}} The use of font families will allow the in-line switching between text states and make it unnecessary to use #select_font to change between these fonts. This will also ensure that the encoding and differences (see “” below) for the selected font family will be consistent. The default font families detailed above (for Helvetica, Times Roman, and Courier) have already been defined for PDF::Writer. 3EmbeddingFonts PDF::Writer will properly embed both TrueType and PostScript Type 1 fonts, but these fonts will require Adobe Font Metrics (AFM) files (extension .afm). These files should exist for PostScript Type 1 files but may not exist for TrueType files on any given system. 4Type1Fonts PostScript Type 1 fonts are fully supported by PDF, but only the binary (.pfb) forms of Type 1 fonts may be embedded into a PDF document. If only the ASCII (.pfa) form of the Type 1 font is available, then the program “t1binary” available as part of the Type 1 utilities package found at the LCDF type software page will create the binary form from the ASCII form. There is a suggestion on that page that older Macintosh Type 1 fonts will need additional conversion with one of the provided utilities. 4TrueTypeFonts AFM files can be generated for TrueType fonts with the program “ttf2afm”, which is part of the package pdfTex by Han The Thanh. In a future release of PDF::Writer or another program from the Ruby PDF project, this requirement should be eliminated. 4 As noted above, PDF::Writer will embed Type 1 or TrueType font programs in the PDF document. Fonts are recognised as copyrightable intellectual property in some jurisdictions. TrueType fonts encode some licensing rights in the font programs and PDF::Writer will check and warn if fonts not licensed for inclusion in a document are selected. It is up to the users of PDF::Writer to ensure that there are no licence violations of fonts embedded in the generated PDF documents. 3SpecialCharacters Fonts in PDF documents can include encoding information. The PDF standard encodings are ‘none’, ‘WinAnsiEncoding’, ‘MacRomanEncoding’, or ‘MacExpertEncoding’. Symbolic fonts should be encoded with the ‘none’ encoding. The default encoding used in PDF::Writer is ‘WinAnsiEncoding’. ‘WinAnsiEncoding’ encoding is not quite the same as Windows code page 1252 (roughly equivalent to latin 1). Appendix D of the Adobe PDF Reference version 1.6 contains full information on all encoding mappings (including mappings for the two included symbolic fonts). 4CharMaps The main way of providing particular character support with PDF::Writer is through a differences map, or a character subtitution table. This is done only when initially selecting the font; it will not be reapplied after the font has been loaded from disk once (this limitation applies to the fourteen built-in fonts as well). .code {{{ encoding = { :encoding => "WinAnsiEncoding", :differences => { 215 => "multiply", 148 => "copyright", } } pdf.select_font("Helvetica", encoding) .endcode }}} The above code maps the bytes 215 (0xd7) and 148 (0x94) to the named characters “multiply” and “copyright” in the Helvetica font. These byte values are the characters “©” and “×” in Windows 1252 but are undefined in the font and therefore require additional information to present and space these characters properly. #TODO As of PDF::Writer version 1.1, these difference maps will be inserted into the PDF documents as well. This change is experimental but should be safe. 4 PDF supports UTF-16BE encoding of strings—but each such string must begin with the UTF-16BE byte order mark (BOM) of U+FEFF (0xFE followed by 0xFF). PDF::Writer does not, at this point, explicitly support either UTF-8 or UTF-16BE strings. If all of the conditions are correct, the following code should display the Japanese characters for “Nihongo” (the name of the Japanese language). As of 2005.05.02, this will not work (at least in this manual). .code {{{ pdf.text("\xfe\xff\x65\xe5\x67\x2c\x8a\x9e") .endcode }}} # .eval {{{ # pdf.text("\xfe\xff\x65\xe5\x67\x2c\x8a\x9e", :justification => :center) # .endeval }}} There are additional limitations and features for Unicode support, but a later version of PDF::Writer will support Unicode (at least UTF-8) documents. .newpage 1WorkingWith The PDF::Writer class is used to create PDF documents in Ruby. It is a “smart” writing canvas on which both text and graphics may be drawn. covers the methods that are used to draw graphics in PDF::Writer. This chapter covers text drawing and most other PDF document operations. The canvas provided by PDF::Writer is described as a “smart” canvas because it is aware of common text writing conventions including page margins and multi-column output. 2CreatingDocuments There are two ways to create PDF documents with PDF::Writer. Both will provide instances of PDF::Writer; these are PDF::Writer.new and PDF::Writer.prepress. 3 This method creates a new PDF document as a writing canvas. Without any parameters, it will create a PDF version 1.3 document that uses pages that are standard US Letter (8½” × 11”) in portrait orientation. It accepts three named parameters, :paper, :version, and :orientation. .code {{{ require "pdf/writer" # US Letter, portrait, 1.3 PDF::Writer.new # 1. A4, portrait, 1.3 PDF::Writer.new(:paper => "A4") # 2. 150cm × 200cm, portrait, 1.3 PDF::Writer.new(:paper => [ 150, 200 ]) # 3. 150pt × 200pt, portrait, 1.3 PDF::Writer.new(:paper => [ 0, 0, 150, 200 ]) # US Letter, landscape, 1.3 PDF::Writer.new(:orientation => :landscape) # US Letter, portrait, 1.5 PDF::Writer.new(:version => PDF_VERSION_15) .endcode }}} The :paper parameter specifies the size of a page in PDF::Writer. It may be specified as: (1) a standard paper size name (see the table “PDF::Writer Page Sizes” below for the defined paper size names); (2) a [width, height] array measured in centimetres; or (3) a [x0, y0, x1, y1] array measured in points where (x0, y0) represents the lower left-hand corner and (x1, y1) represent the upper right-hand corner of the page. The :orientation parameter specifies whether the pages will be :portrait (the long edge is the height of the page) or :landscape (the long edge is the width of the page). These are the only allowable values. The :version parameter specifies the minimum specification version that the document will adhere to. As of this version, the PDF document version is inserted but not used to control what features may be inserted into a document. The latest version of the PDF specification is PDF 1.6 (associated with Adobe Acrobat 7); it is recommended that the default version (1.3) be kept in most cases as that will ensure that most users will have access to the features in the document. A later version of PDF::Writer will include version controls so that if when creating a PDF 1.3 document, features from PDF 1.6 will not be available. PDF::Writer currently supports features from PDF 1.3 and does not yet provide access to the advanced features of PDF 1.4 or higher. .eval {{{ pdf.select_font("Helvetica") PDF::SimpleTable.new do |tab| tab.title = "PDF::Writer Page Sizes" tab.column_order.replace %w(name1 size1 name2 size2) tab.columns["name1"] = PDF::SimpleTable::Column.new("name1") { |col| col.heading = PDF::SimpleTable::Column::Heading.new("Type") } tab.columns["size1"] = PDF::SimpleTable::Column.new("size1") { |col| col.heading = PDF::SimpleTable::Column::Heading.new("Size") } tab.columns["name2"] = PDF::SimpleTable::Column.new("name2") { |col| col.heading = PDF::SimpleTable::Column::Heading.new("Type") } tab.columns["size2"] = PDF::SimpleTable::Column.new("size2") { |col| col.heading = PDF::SimpleTable::Column::Heading.new("Size") } tab.show_lines = :all tab.show_headings = true tab.orientation = :center tab.position = :center data = [ { "name1" => "2A0", "size1" => '46.811" x 66.220" (118.9cm x 168.2cm)', "name2" => "4A0", "size2" => '66.220" x 93.622" (168.2cm x 237.8cm)' }, { "name1" => "A0", "size1" => '33.110" x 46.811" (84.1cm x 118.9cm)', "name2" => "A1", "size2" => '23.386" x 33.110" (59.4cm x 84.1cm)' }, { "name1" => "A2", "size1" => '16.535" x 23.386" (42cm x 59.4cm)', "name2" => "A3", "size2" => '11.693" x 16.535" (29.7cm x 42cm)' }, { "name1" => "A4", "size1" => '8.268" x 11.693" (21cm x 29.7cm)', "name2" => "A5", "size2" => '5.827" x 8.268" (14.8cm x 21cm)' }, { "name1" => "A6", "size1" => '4.134" x 5.827" (10.5cm x 14.8cm)', "name2" => "A7", "size2" => '2.913" x 4.134" (7.4cm x 10.5cm)' }, { "name1" => "A8", "size1" => '2.047" x 2.913" (5.2cm x 7.4cm)', "name2" => "A9", "size2" => '1.457" x 2.047" (3.7cm x 5.2cm)' }, { "name1" => "A10", "size1" => '1.024" x 1.457" (2.6cm x 3.7cm)' }, { "name1" => "B0", "size1" => '39.370" x 55.669" (100cm x 141.4cm)', "name2" => "B1", "size2" => '27.835" x 39.370" (70.7cm x 100cm)' }, { "name1" => "B2", "size1" => '19.685" x 27.835" (50cm x 70.7cm)', "name2" => "B3", "size2" => '13.898" x 19.685" (35.3cm x 50cm)' }, { "name1" => "B4", "size1" => '9.842" x 13.898" (25cm x 35.3cm)', "name2" => "B5", "size2" => '6.929" x 9.842" (17.6cm x 25cm)' }, { "name1" => "B6", "size1" => '4.921" x 6.929" (12.5cm x 17.6cm)', "name2" => "B7", "size2" => '3.465" x 4.921" (8.8cm x 12.5cm)' }, { "name1" => "B8", "size1" => '2.441" x 3.465" (6.2cm x 8.8cm)', "name2" => "B9", "size2" => '1.732" x 2.441" (4.4cm x 6.2cm)' }, { "name1" => "B10", "size1" => '1.220" x 1.732" (3.1cm x 4.4cm)' }, { "name1" => "C0", "size1" => '36.102" x 51.063" (91.7cm x 129.7cm)', "name2" => "C1", "size2" => '25.512" x 36.102" (64.8cm x 91.7cm)' }, { "name1" => "C2", "size1" => '18.032" x 25.512" (45.8cm x 64.8cm)', "name2" => "C3", "size2" => '12.756" x 18.032" (32.4cm x 45.8cm)' }, { "name1" => "C4", "size1" => '9.016" x 12.756" (22.9cm x 32.4cm)', "name2" => "C5", "size2" => '6.378" x 9.016" (16.2cm x 22.9cm)' }, { "name1" => "C6", "size1" => '4.488" x 6.378" (11.4cm x 16.2cm)', "name2" => "C7", "size2" => '3.189" x 4.488" (8.1cm x 11.4cm)' }, { "name1" => "C8", "size1" => '2.244" x 3.189" (5.7cm x 8.1cm)', "name2" => "C9", "size2" => '1.575" x 2.244" (4.0cm x 5.7cm)' }, { "name1" => "C10", "size1" => '1.102" x 1.575" (2.8cm x 4.0cm)' }, { "name1" => "EXECUTIVE", "size1" => '7.248" x 10.5" (18.410cm x 26.670cm)', "name2" => "FOLIO", "size2" => '8.5" x 13" (21.590cm x 33.020cm)' }, { "name1" => "LEGAL", "size1" => '8.5" x 14" (21.590cm x 35.560cm)', "name2" => "LETTER", "size2" => '8.5" x 11" (21.590cm x 27.940cm)' }, { "name1" => "RA0", "size1" => '33.858" x 48.032" (86cm x 122cm)', "name2" => "RA1", "size2" => '24.016" x 33.858" (61cm x 86cm)' }, { "name1" => "RA2", "size1" => '16.929" x 24.016" (43cm x 61cm)', "name2" => "RA3", "size2" => '12.008" x 16.929" (30.5cm x 43cm)' }, { "name1" => "RA4", "size1" => '8.465" x 12.008" (21.5cm x 30.5cm)' }, { "name1" => "SRA0", "size1" => '35.433" x 50.394" (90cm x 128cm)', "name2" => "SRA1", "size2" => '25.197" x 35.433" (64cm x 90cm)' }, { "name1" => "SRA2", "size1" => '17.717" x 25.197" (45cm x 64cm)', "name2" => "SRA3", "size2" => '12.598" x 17.717" (32cm x 45cm)' }, { "name1" => "SRA4", "size1" => '8.858" x 12.598" (22.5cm x 32cm)' } ] tab.data.replace data tab.render_on(pdf) end .endeval }}} 3 This is an alternative way to create a new PDF document. In addition to the named parameters in PDF::Writer.new (:paper, :orientation, and :version), this method accepts the following options as well. :left_margin, :right_margin, :top_margin, and :bottom_margin specify the margins. Prepress marks are placed relative to the margins, so when creating a document in prepress mode the margins must be specified as the document is created. Future versions of PDF::Writer will be more flexible on this. The default margins are 36pts (about ½”). :bleed_size and :mark_length specify the size of the bleed area (default is 12 points) and the length of the prepress marks (default is 18 points). Prepress marks will appear on all pages. .code {{{ require "pdf/writer" PDF::Writer.prepress # US Letter, portrait, 1.3, prepress .endcode }}} 2AddingText There are two different and complementary ways of adding text to a PDF document with PDF::Writer. The easier is to use the generated PDF as a “smart” canvas, where information internal to the document is used to maintain a current writing pointer (see “”) and text is written within the writing space defined by document margins (see “”). The harder is to place the text on the document canvas explicitly, ensuring that the text does not overflow the page horizontally or vertically. In order to support more flexible and humane layout, PDF::Writer supports XML-like tags that can be used to change the current text state (discussed earlier in “”), substitute alternate text, or apply custom formatting. This is discussed in detail in “”. 3TextWrapping PDF::Writer uses a very simple text wrapping formula. If a line looks like it’s going to exceed its permitted width, then PDF::Writer backtracks to the previous hyphen (‘-’) or space to see if the text will then fit into the permitted width. It will do this until the text will fit. If no acceptable hyphen or space can be found, the text will be forcibly split at the largest position that will fit in the permitted width. 3<“Smart” Text Writing> As noted, PDF::Writer has two different ways to write text to a PDF document. This is expressed with three different methods. The method that uses the PDF document as a “smart” canvas is PDF::Writer#text. 4 This method will add text to the document starting at the current drawing position (described in “”). It uses the document margins (see “”) to flow text onto the page. If an explicit newline (‘\n’) is encountered, it will be used. The text is drawn from the left margin until it reaches the right margin. If the text exceeds the right margin, then the text will be wrapped and continued on the next line. If the text to be written will exceed the bottom margin, then a new page will be created (detailed in “”). .code {{{ pdf.text("Ruby is fun!") .endcode }}} 5 If the :font_size is not specified, then the last font size or the default font size of 12 points will be used. If this value is provided, this changes the current font size just as if the #font_size attribute were set. .code {{{ pdf.text("Ruby is fun!", :font_size => 16) .endcode }}} 5TextJustification Text may be justified within the margins (normal or overridden) in one of four ways with the option :justification. .blist disc {{{ :left justification is also called “ragged-right”, because the text is flush against the left margin and the right edge of the text is uneven. This is the default justification. :right justification places the text flush agains the right margin; the left edge of the text is uneven. :center justification centers the text between the margins. :full justification ensures that the text is flush against both the left and right margins. Additional space is inserted between words to make this happen. The last line of a paragraph is only made flush left. .endblist }}} .code {{{ pdf.text("Ruby is fun!", :justification => :left) pdf.text("Ruby is fun!", :justification => :right) pdf.text("Ruby is fun!", :justification => :center) pdf.text("Ruby is fun!", :justification => :full) .endcode }}} 5 The drawing positions and margins may be modified with the :left, :right, :absolute_left, and :absolute_right options. The :left and :right values are PDF userspace unit offsets from the left and right margins, respectively; the :absolute_left and :absolute_right values are userspace units effectively providing new margins for the text to be written. Note that :absolute_right is offset from the left side of the page, not the right side of the page. .code {{{ # The left margin is shifted inward 50 points. pdf.text("Ruby is fun!", :left => 50) # The right margin is shifted inward 50 points. pdf.text("Ruby is fun!", :right => 50) # The text is drawn starting at 50 points from the left of the page. pdf.text("Ruby is fun!", :absolute_left => 50) # The text is drawn ending at 300 points from the left of the page. pdf.text("Ruby is fun!", :absolute_right => 300) .endcode }}} 5 Normally, each line of text is separated only by the descender space for the font. This means that each line will be separated by only enough space to keep it readable. This may be changed by changing the line spacing (:spacing) as a multiple of the normal line height (authors’ manuscripts are often printed double spaced or :spacing => 2). This can also be changed by redefining the total height of the line (the leading) independent of the font size. With :font_size => 12, both :spacing => 2 and :leading => 24 do the same thing. The :leading value overrides the :spacing value. .code {{{ # These are the same pdf.text("Ruby is fun!", :spacing => 2) pdf.text("Ruby is fun!", :leading => 24) .endcode }}} 5 Not generally used, the :test option can be set to true. This will prevent the text from being written to the page and the method will return true if the text will be overflowed to a new page or column. 3 Text can also be placed starting with specific X and Y coordinates using either PDF::Writer#add_text_wrap or PDF::Writer#add_text. 4 This will add text to the page at position (x, y), but ensures that it fits within the provided width. If it does not fit, then as much as possible will be written using the wrapping rules on page . The remaining text to be written will be returned to the caller. .code {{{ rest = pdf.add_text_wrap(150, pdf.y, 150, "Ruby is fun!", 72) .endcode }}} If size is not specified, the current #font_size will be used. This parameter has changed position with text in PDF::Writer 1.1. If your code appears to be using the old parameter call, it will be warned. The optional justification parameter works just as described on page , except that instead of margin width, it is the boundaries defined by the x and width parameters. Text may be drawn at an angle. This will be discussed fully in the section on PDF::Writer#add_text. If the test parameter is true, the text will not be written, but the remainder of the text will be returned. 4 This places the full text string at the (x, y) position and the specified angle (measured in degrees of a circle, in a counter-clockwise direction) with respect to the angle of the coordinate axis. NOTE: As of PDF::Writer 1.1.0, angle rotations are now counter-clockwise, not clockwise as in earlier versions. This is a necessary incompatible change to make transformations more compatible with other vector graphics systems. If size is not specified, the current #font_size will be used. This parameter has changed position with text in PDF::Writer 1.1. If your code appears to be using the old parameter call, it will be warned. .code {{{ 0.step(315, 45) do |angle| pdf.add_text(pdf.margin_x_middle, pdf.y, "#{angle}º".rjust(8), 12, angle) end .endcode }}} .eval {{{ x = pdf.margin_x_middle y = pdf.y - 50 0.step(315, 45) do |angle| pdf.add_text(x, y, "#{angle}º".rjust(8), 12, angle) end pdf.y = y - 30 .endeval }}} The adjust parameter adjusts the space between words by the specified number of PDF userspace units. This is primarily used by PDF::Writer#text and PDF::Writer#add_text_wrap to support text justification. 3TextTags Text is written in PDF documents as simple strings positioned on the page canvas. Unlike HTML, there is no easy way to indicate that the font or the font size should be changed. This has to be managed entirely by the generating application. PDF::Writer has a mechanism called “text tags” to manage these changes and provide hooks to introduce new behaviours. PDF::Writer provides five classes of text tags. Two are used to change font families and have fixed meaning; the other three are callback tags. Because text tags use XML semantics, XML escape characters are necessary to display tag characters. .blist {{{ &lt; => < &gt; => > &amp; => & .endblist }}} 4 The font family tags have fixed meaning and will add the style identifiers ‘b’ and ‘i’ to the current text state, respectively. Both require closing tags. The text between the tags will be rendered in bold, italic, or bold italic versions of the master font as appropriate. If the font does not exist, then the master font is used: PDF::Writer will not simulate italics or boldface. See for an explanation of font families and text state. 4 The remaining three tag classes are callback tags. All callback tags have a regular form of <x:name[ parameters]>. This is essentially the form of a namespaced XML tag. Parameters must be specified as with XML, in the form of name="value" or name='value'. Parameters will be provided to callbacks as a name-value hash: .code {{{ <c:alink uri="http://rubyforge.org/">RubyForge</c:alink> .endcode }}} When the opening c:alink tag is encountered, the callback will receive a tag parameter hash of: .code {{{ { "uri" => "http://rubyforge.org/" } .endcode }}} When the closing c:alink tag is encountered, the callback will receive an empty parameter hash ({}). 4 The constant hash PDF::Writer::TAGS has three hashes that keep track of tag name and callback class associations. A sample set of associations might be: .code {{{ TAGS = { :pair => { "alink" => PDF::Writer::TagAlink, }, :single => { "disc" => PDF::Writer::TagDisc, }, :replace => { "xref" => PDF::TechBook::TagXref, }, } .endcode }}} Callback tags must define #[](pdf, params). The standard callback classes define CallbackClass.[](pdf, params) so that the callbacks are used without instantiation. 4 Replacement tags will replace the tag with a value computed by the callback. It may perform additional processing, but no location information is provided to the callback. The return from this callback must be the string that will replace the tag. A replacement tag looks like this in text: .code {{{ <r:xref name="FontFamilies" label="title" /> .endcode }}} Replacement tags always begin with the “namespace” of ‘r:’ and do not surround text (they are stand-alone tags). The params parameter sent to #[] is the default parameters hash, containing only information from the tag itself. 5 The example below is the “xref” tag from PDF::TechBook. .code {{{ class TagXref def self.[](pdf, params) name = params["name"] item = params["label"] xref = pdf.xref_table[name] if xref case item when 'page' label = xref[:page] || xref[:label] when 'label' label = xref[:label] end "<c:ilink dest='#{xref[:xref]}'>#{label}</c:ilink>" else warn PDF::Writer::Lang[:techbook_unknown_xref] % [ name ] PDF::Writer::Lang[:techbook_unknown_xref] % [ name ] end end end PDF::Writer::TAGS[:replace]["xref"] = PDF::TechBook::TagXref .endcode }}} 4 Single drawing tags are stand-alone tags that will perform drawing behaviours when the tag is encountered. Location information is provided to the callback and the callback may return a hash with (x, y) information to adjust the position of following text. A single drawing tag looks like this in text: .code {{{ <C:bullet /> .endcode }}} Single drawing tags always begin with the “namespace” of ‘C:’ and do not surround text. The params parameter sent to #[] is a complex compound object. See for more information on the parameters provided to drawing tags. A single drawing tag callback will be called once and only once when it is encountered. 5 The example below is the “bullet” tag from PDF::Writer. .code {{{ class TagBullet DEFAULT_COLOR = Color::RGB::Black class << self attr_accessor :color def [](pdf, info) @color ||= DEFAULT_COLOR desc = info[:descender].abs xpos = info[:x] - (desc * 2.00) ypos = info[:y] + (desc * 1.05) pdf.save_state ss = StrokeStyle.new(desc) ss.cap = :butt ss.join = :miter pdf.stroke_style ss pdf.stroke_color @style pdf.circle_at(xpos, ypos, 1).stroke pdf.restore_state end end end TAGS[:single]["bullet"] = TagBullet .endcode }}} 4 Paired drawing tags are tags that surround text and perform drawing behaviours realted to that text when the tag is encountered. Location information is provided to the callback and the callback may return a hash with (x, y) information to adjust the position of following text. A paire ddrawing tag looks like this in text: .code {{{ <c:uline>text</c:uline> .endcode }}} Paired drawing tags always begin with the “namespace” of ‘c:’ and surround text. The params parameter sent to #[] is a complex compound object. See for more information on the parameters provided to drawing tags. A paired drawing tag callback will be called at least twice, and may be called many times while it is open. It will be called once when the tag is opened, once for every line “end” that is reached, once for every line “start” that is reached, and once when the tag is closed. For PDF::Writer#text, line start and end positions are whenever a natural or wrapped newline is encountered. For PDF::Writer#add_text_wrap and PDF::Writer#add_text, the line start and end are used for each call while a tag is open. 5 The example below is the “uline” tag from PDF::Writer. .code {{{ class TagUline DEFAULT_STYLE = { :factor => 0.05 } class << self attr_accessor :style def [](pdf, info) @style ||= DEFAULT_STYLE.dup case info[:status] when :start, :start_line @links ||= {} @links[info[:cbid]] = { :x => info[:x], :y => info[:y], :angle => info[:angle], :descender => info[:descender], :height => info[:height], :uri => nil } pdf.save_state pdf.stroke_style! StrokeStyle.new(info[:height] * @style[:factor]) when :end, :end_line start = @links[info[:cbid]] theta = PDF::Math.deg2rad(start[:angle] - 90.0) drop = start[:height] * @style[:factor] * 1.5 drop_x = Math.cos(theta) * drop drop_y = -Math.sin(theta) * drop pdf.move_to(start[:x] - drop_x, start[:y] - drop_y) pdf.line_to(info[:x] - drop_x, info[:y] - drop_y).stroke pdf.restore_state end end end end TAGS[:pair]["uline"] = TagUline .endcode }}} 4DrawingTagParameters Drawing tags expect more information than is provided to replacement tags, as they are expected to draw something or perform other tasks on the document at the time that they are encountered in the appropriate position(s). The parameters provided are described below. 5<:x> The current x position of the text. 5<:y> The current y position of the text. 5<:angle> The current angle of the text in degrees. This will allow the correct calculation of text or drawing orientation. 5<:params> The hash of named parameters for the tag. This is the same as the value that is provided to replacement tags. 5<:status> One of the values :start, :end, :start_line, or :end_line .blist {{{ :start is provided to the callback when encountering the opening tag for a paired drawing tag, or when encountering a single drawing tag. :end is provided to the callback when the closing tag for a paired drawing tag. :start_line is provided to the callback when a new line is to be drawn and a paired tag is open. :end_line is provided to the callback when a line is finished writing and a paired tag is open. .endblist }}} 5<:cbid> The callback identifier; this may be used as a key into a variable which keeps state for the various callbacks. In the “uline” example, the @links variable keeps this information. 5<:callback> The name of the tag, used to find the callback object. This is only set for opening paired tags or single tags. 5<:height> The font height at the time the tag was encountered. 5<:descender> The font descender at the time the tag was encountered. 4 A few useful text tags and their callbacks have been defined in PDF::Writer. Two others are used by the PDF::TechBook class used to generate this manual. 5<<c:alink uri="URI">> The <c:alink uri="URI"> tag is used to make a link to a resource external to the document. This can be any URL handler registered by the operating system for processing. The text “<c:alink uri="http://rubyforge.org/">RubyForge</c:alink>” will generate a hyperlink like “RubyForge”. This is known to work with HTTP, HTTPS, FTP, and MAILTO style URIs. The callback object is PDF::Writer::TagAlink. The display style of linking and link underlining may be modified through TagAlink.style, which is a hash of five keys: .blist {{{ :color is colour of the link underline. The default is Color::RGB::Blue. If nil, the current stroke colour will be maintained. :text_color is the colour of the text. The default is Color::RGB::Blue. If nil, the current fill colour will be maintained. :factor is the size of the line, as a multiplier of the text height. The default is 5% of the line height (0.05). :line_style is a style modification hash provided to PDF::Writer::StrokeStyle.new. The default is a solid line with normal cap, join, and miter limit values. See “” for more information. :draw_line indicates whether the underline should be drawn or not. .endblist }}} 5<<c:ilink dest="DEST">> The <c:ilink dest="DEST"> tag is used to make a link to a destination within the document. Destinations must be created manually. .code {{{ # Code writing ... # ... text, pages, etc. pdf.add_destination("x3y3z3", "Fit") # More code writing ... # ... text, pages, etc. pdf.text("<c:ilink dest='x3y3z3'>Internal Link</c:ilink>") .endcode }}} This manual contains an extensive cross-reference table. This link will goes to the fifth item in that cross-reference table. (The code was <c:ilink dest="xref5">This link</c:ilink>.) This is implemented with PDF::Writer::TagIlink. There are no configuration options. 5<<c:uline>> The <c:uline> tag is used to underline text between the opening tag and the closing tag. Therefore, “The quick brown fox <c:uline>is tired of</c:uline> jumping over the lazy dog” becomes “The quick brown fox is tired of jumping over the lazy dog”. This is implemented with PDF::Writer::TagUline. The display style of linking and link underlining may be modified through TagUline.style, which is a hash of three keys: .blist {{{ :color is colour of the link underline. The default is nil, which means the current stroke colour will be used. :factor is the size of the line, as a multiplier of the text height. The default is 5% of the line height (0.05). :line_style is a style modification hash provided to PDF::Writer::StrokeStyle.new. The default is a solid line with normal cap, join, and miter limit values. See “” for more information. .endblist }}} 5<<C:bullet>> The <C:bullet> tag inserts a solid circular bullet, like this: “ ”. Bullets are implemented in PDF::Writer::TagBullet. The bullet display colour may be modified through TagBullet.color, which defaults to Color::RGB::Black. 5<<C:disc>> The <C:disc> tag inserts a hollow circular bullet, like this: “ ”. Bullets are implemented in PDF::Writer::TagDisc. The disc display colours may be modified throguh TagDisc.foreground and TagDisc.background, which default to Color::RGB::Black and Color::RGB::White, respectively. 5<<C:tocdots ...>> This is a stand-alone callback that draws a dotted line over to the right and appends a page number; it is implemented and used in PDF::TechBook::TagTocDots. It is of limited configurability in this version. 5<<r:xref name="XREFNAME" label="page|label" text="TEXT"/>> This replacement callback returns an internal link (<c:ilink...>) with either the name (label="title") or the page number (label="page") of the cross-reference, or an indicator for arbitrary text (label="text") drawn from the text attribute (e.g., text="random text here"). The page number will only be used if it is known at the time that the <r:xref> tag is processed; forward cross-references will always use the text (if present) or the label. This is implemented through PDF::TechBook::TagXref. 3 These methods return the height of the font or the width of the text. 4 Returns the height of the current font for the given size, measured in PDF userspace units. This is the distance from the bottom of the descender to the top of the capitals or ascenders. Uses the current #font_size if size is not provided. 4 Returns the size of the descender—the distance below the baseline of the font—which will normally be a negative number. Uses the current #font_size if size is not provided. 4 4 Returns the width of the given text string at the given font size using the current font, or the current default font size if none is given. The difference between #text_width and #text_line_width is that the former will give the width of the largest line in a multiline string. 2DocumentMargins A document canvas should not be written from edge to edge in most cases; printers that try to print these items will often lose portions. From a design perspective, margins increase the amount of whitespace on the page and make the page easier to read. Drawing methods that do not use the internal writing pointer (see below) will not use the margin. Typically, these are methods that explicitly specify (x, y) coordinates. The default margins are 36pts (about ½”). 3 PDF::Writer provides four methods to quickly define all margins at once. With all four of these methods, if only one value is provided, all four margins are that value. Two values define the top/bottom margins and the left/right margins. Three values set separate top/bottom margins, but the left/right margins will be the same. Four values defines each margin independently, as shown below. The only difference between the methods is the measurements used. .code {{{ # T L B R pdf.margins_pt(36) # 36 36 36 36 pdf.margins_pt(36, 54) # 36 54 36 54 pdf.margins_pt(36, 54, 72) # 36 54 72 54 pdf.margins_pt(36, 54, 72, 90) # 36 54 72 90 .endcode }}} 4 Set margins in points. 4 Set margins in centimetres. 4 Set margins in millimetres. 4 Set margins in inches. 4 4 4 4 In addition, each margin may be examined and modified independently, but all measurements for direct margin access are in PDF userspace units only. .code {{{ pdf.top_margin # -> 36 pdf.top_margin = 72 # Also #left_margin, #bottom_margin, #right_margin .endcode }}} 3 Margin values in PDF::Writer are offset values. If the right margin is ½”, the absolute position of the right margin will be the width of the page less the offset of the right margin (conventionally, it will be the same as “pdf.page_width - pdf.right_margin”. PDF::Writer provides attributes to read these and other page dimension values. All measurements are in PDF userspace units. 4 The width of the page. 4 The height of the page. 4 The absolute horizontal position of the left margin. 4 The absolute horizontal position of the right margin. 4 The absolute vertical position of the top margin. 4 The absolute vertical position of the bottom margin. 4 The height of the writing space. 4 The width of the writing space. 4 The horizontal middle of the page based on the page dimensions. 4 The vertical middle of the page based on the page dimensions. 4 The horizontal middle of the page based on the margin dimensions. 4 The vertical middle of the page based on the margin dimensions. .code {{{ x = pdf.page_width - pdf.right_margin # flush right y = pdf.page_height - pdf.top_margin # flush top # Draw a box at the margin positions. x = pdf.absolute_left_margin w = pdf.absolute_right_margin - x # or pdf.margin_width y = pdf.absolute_bottom_margin h = pdf.absolute_top_margin - y # or pdf.margin_height pdf.rectangle(x, y, w, h).fill .endcode }}} 2WritingPointer PDF::Writer keeps an internal writing pointer for use with several different (mostly text) drawing methods. There are several ways to manipulate this pointer directly. The writing pointer value is changed automatically with drawing methods, column support, and starting new pages with #start_new_page. 3 The vertical position of the writing point. The vertical position is constrained between the top and bottom margins. Any attempt to set it outside of those margins will cause the writing point to be placed absolutely at the margins. .code {{{ pdf.y # => 40 pdf.bottom_margin # => 36 pdf.y = 30 # => 36 .endcode }}} 3 The vertical position of the writing point. If the vertical position is outside of the bottom margin, a new page will be created. .code {{{ pdf.pageset.size # => 1 pdf.pointer # => 40 pdf.bottom_margin # => 36 pdf.top_margin # => 36 pdf.page_height # => 736 pdf.pointer = 30 pdf.pageset.size # => 2 pdf.pointer # => 700 .endcode }}} 3 Used to change the vertical position of the writing point. The pointer is moved down the page by dy. If the pointer is to be moved up, a negative number must be used. Moving up the page will not move to the previous page because of limitations in the way that PDF::Writer works. The writing point will be limited to the top margin position. .code {{{ pdf.move_pointer(10) .endcode }}} If make_space is true and a new page is required to move the pointer, then the pointer will be moved down on the new page. This will allow space to be reserved for graphics. The following will guarantee that there are 100 units of space above the final writing point. .code {{{ move_pointer(100, true) .endcode }}} 2ColumnOutput While it can be said that the PDF document is a canvas, it is a canvas of multiple pages. PDF::Writer does some intelligent management of the document to create pages as text flows off of it, but sometimes it will be necessary or useful to start pages manually. The class also provides facilities to write multi-column output automatically. If a margin-aware drawing method reaches the bottom margin, it will request a new page with #start_new_page. 3 When a PDF::Writer document is first created, an initial page is created and inserted in the document. This page is always accessible with the #first_page method. 4 The first page created during startup, useful for adding something to it later. 3 4 Nominally starts a new page and moves the writing pointer is moved back to the top margin. If multi-column output is on, a new column may be started instead of a new page. If an actual new page is required, the new page is added to the page list according to the page insert options (detailed below). If force is true, then a new page will be created even if multi-column output is on. 4 Add a new page to the document. This also makes the new page the current active object. This allows for mandatory page creation regardless of multi-column output. Note the writing pointer position is not reset with the use of this method. For most purposes, #start_new_page is preferred. 3 These options control where in the page set new pages are inserted when using PDF::Writer#start_new_page. When #insert_mode is on, new pages will be inserted at the specified page. If a page is inserted :before the page, then the #insert_position is changed to :after upon successful insertion of that page. This will ensure that if with a page list like “1 2 3 4 5 6”, inserting a page between pages 3 and 4 with: .code {{{ pdf.insert_mode :on pdf.insert_page 4 pdf.insert_position :before ... # insert pages 7, 8, and 9 .endcode }}} will result in a page list like “1 2 3 7 8 9 4 5 6” and not “1 2 3 9 8 7 4 5 6.” When #insert_mode is off, pages are appended to the end of the document. 4 Changes page insert mode. May be called as follows: .code {{{ pdf.insert_mode # => current insert mode # The following four affect the insert mode without changing the insert # page or insert position. pdf.insert_mode(:on) # enables insert mode pdf.insert_mode(true) # enables insert mode pdf.insert_mode(:off) # disables insert mode pdf.insert_mode(false) # disables insert mode # Changes the insert mode, the insert page, and the insert position at the # same time. This is the same as calling: # # pdf.insert_mode(:on) # pdf.insert_page(:last) # pdf.insert_position(:before) opts = { :on => true, :page => :last, :position => :before } pdf.insert_mode(opts) .endcode }}} 4 Returns or changes the insert page property. The page must be either :last or an integer value representing the position in the page set—this value is completely unrelated to any page numbering scheme that may be currently in progress. .code {{{ pdf.insert_page # => current insert page pdf.insert_page(35) # insert at page 35 pdf.insert_page(:last) # insert at the last page .endcode }}} 4 Returns or changes the insert position to be before or after the insert page. .code {{{ pdf.insert_position # => current insert position pdf.insert_position(:before) # insert before #insert_page pdf.insert_position(:after) # insert before #insert_page .endcode }}} 3 4 Starts multi-column output. Creates size number of columns with a gutter of PDF unit space between each column. If columns are already started, this will return false as only one level of column definitions may be active at any time. .code {{{ pdf.start_columns pdf.start_columns(3) pdf.start_columns(3, 2) pdf.start_columns(2, 20) .endcode }}} When columns are on, #start_new_page will start a new column unless the last column is the current writing space, where it will create a new page. 4 Turns off multi-column output. If we are in the first column, or the lowest point at which columns were written is higher than the bottom of the page, then the writing pointer will be placed at the lowest point. Otherwise, a new page will be started. 4 Returns the width of the currently active column or 0 if columns are off. 4 Returns the size of the gutter between columns or 0 if columns are off. 4 Returns the current column number or 0 if columns are off. Column numbers are 1-indexed. 4 Returns the total number of columns or 0 if columns are off. 4 Returns true if columns are turned on. 2PageNumbering PDF::Writer supports automatic page numbering. The current page numbering system does not support anything except standard Arabic numbering (e.g., 1, 2, 3). The page numbering mechanism will be changing in a future version of PDF::Writer to be far more flexible. 4 Prepares the placement of page numbers on pages from the current page. Place them relative to the coordinates (x, y) with pos as the relative position. pos may be :left, :right, or :center. The page numbers will be written on each page using pattern. When pattern is rendered, <PAGENUM> will be replaced with the current page number; <TOTALPAGENUM> will be replaced with the total number of pages in the page numbering scheme. The default pattern is “<PAGENUM> of <TOTALPAGENUM>”. If starting is non-nil, this is the first page number. The number of total pages will be adjusted to account for this. Each time page numbers are started, a new page number scheme will be started. The scheme number will be returned for use in other page number methods. The following code produces a ten page document, numbered from the second page (labelled ‘1 of 9’) until the eighth page (labelled ‘7 of 9’). .code {{{ pdf = PDF::Writer.new pdf.select_font "Helvetica" # page 1, blank pdf.start_new_page # page 2, 1 of 9 pdf.start_page_numbering(300, 500, 20, nil, nil, 1) pdf.start_new_page # page 3, 2 of 9 pdf.start_new_page # page 4, 3 of 9 pdf.start_new_page # page 5, 4 of 9 pdf.start_new_page # page 6, 5 of 9 pdf.start_new_page # page 7, 6 of 9 pdf.start_new_page # page 8, 7 of 9 pdf.stop_page_numbers pdf.start_new_page # page 9, blank pdf.start_new_page # page 10, blank .endcode }}} Multiple page numbering schemes can be used on the same page, as demonstrated in demo/pagenumber.rb or below. .code {{{ pdf = PDF::Writer.new # Page 1: blank sa = pdf.start_page_numbering(5, 60, 9, nil, nil, 1) pdf.start_new_page # Page 2: 1 of 2 pdf.start_new_page # Page 3: 2 of 2 pdf.stop_page_numbering(true, :current, sa) pdf.start_new_page # Page 4: blank sb = pdf.start_page_numbering(5, 50, 9, :center, nil, 10) pdf.start_new_page # Page 5: 10 of 12 pdf.start_new_page # Page 6: 11 of 12 pdf.stop_page_numbering(true, :next, sb) pdf.start_new_page # Page 7: 12 of 12 sc = pdf.start_page_numbering(5, 40, 9, nil, nil, 1) pdf.start_new_page # Page 8: 1 of 3 pdf.start_new_page # Page 9: 2 of 3 pdf.start_new_page # Page 10: 3 of 3 pdf.stop_page_numbering(true, :current, sc) pdf.start_new_page # Page 11: blank sd = pdf.start_page_numbering(5, 30, 9, nil, nil, 1) pdf.start_new_page # Page 12: 1 of 6 pdf.start_new_page # Page 13: 2 of 6 se = pdf.start_page_numbering(5, 20, 9, nil, nil, 5) sf = pdf.start_page_numbering(5, 10, 9, :right, nil, 1) pdf.start_new_page # Page 14: 3 of 6, 5 of 10, 1 of 8 pdf.start_new_page # Page 15: 4 of 6, 6 of 10, 2 of 8 pdf.start_new_page # Page 16: 5 of 6, 7 of 10, 3 of 8 pdf.stop_page_numbering(true, :next, sd) pdf.start_new_page # Page 17: 6 of 6, 8 of 10, 4 of 8 pdf.start_new_page # Page 18: 9 of 10, 5 of 8 pdf.stop_page_numbering(true, :next, se) pdf.stop_page_numbering(false, :current, sf) pdf.start_new_page # Page 19: 10 of 10 pdf.start_new_page # Page 20: blank .endcode }}} .eval {{{ pdf.select_font("Helvetica") data = [ { "p1" => 1, "c1" => "blank", "p2" => 6, "c2" => "11 of 12", "p3" => 11, "c3" => "blank", "p4" => 16, "c4" => "5 of 6, 7 of 10, 3 of 8", }, { "p1" => 2, "c1" => "1 of 2", "p2" => 7, "c2" => "12 of 12", "p3" => 12, "c3" => "1 of 6", "p4" => 17, "c4" => "6 of 6, 8 of 10, 4 of 8" }, { "p1" => 3, "c1" => "2 of 2", "p2" => 8, "c2" => "1 of 3", "p3" => 13, "c3" => "2 of 6", "p4" => 18, "c4" => "9 of 10, 5 of 8", }, { "p1" => 4, "c1" => "blank", "p2" => 9, "c2" => "2 of 3", "p3" => 14, "c3" => "3 of 6, 5 of 10, 1 of 8", "p4" => 19, "c4" => "10 of 10", }, { "p1" => 5, "c1" => "10 of 12", "p2" => 10, "c2" => "3 of 3", "p3" => 15, "c3" => "4 of 6, 6 of 10, 2 of 8", "p4" => 20, "c4" => "blank", }, ] PDF::SimpleTable.new do |tab| tab.column_order.replace %w(p1 c1 p2 c2 p3 c3 p4 c4) tab.columns["p1"] = PDF::SimpleTable::Column.new("p1") { |col| col.heading = "Page" } tab.columns["c1"] = PDF::SimpleTable::Column.new("c1") { |col| col.heading = "Contents" } tab.columns["p2"] = PDF::SimpleTable::Column.new("p2") { |col| col.heading = "Page" } tab.columns["c2"] = PDF::SimpleTable::Column.new("c2") { |col| col.heading = "Contents" } tab.columns["p3"] = PDF::SimpleTable::Column.new("p3") { |col| col.heading = "Page" } tab.columns["c3"] = PDF::SimpleTable::Column.new("c3") { |col| col.heading = "Contents" } tab.columns["p4"] = PDF::SimpleTable::Column.new("p4") { |col| col.heading = "Page" } tab.columns["c4"] = PDF::SimpleTable::Column.new("c4") { |col| col.heading = "Contents" } tab.data.replace data tab.font_size = 8 tab.render_on(pdf) end .endeval }}} 4 Stops page numbering for the provided scheme. Returns false if page numbering is off. If stop_total is true, then the totaling of pages for this page numbering scheme will be stopped as well. If stop_at is :current, then the page numbering will stop at the current page; otherwise, it will stop at the next page. 4 Given a particular generic page number page_num (numbered sequentially from the beginning of the page set), return the page number under a particular page numbering scheme. Returns nil if page numbering is not turned on. 2 It is common in documents to see items repeated from page to page. These may be watermarks, headers, footers, or other elements that must appear on multiple pages—aside from page numbering. PDF::Writer supports this through a mechanism called “loose content objects.” 3 Up until this point, a page has been presented as the only canavas available to write and draw upon. This is a useful fiction, but it is a fiction. Any contents object may be drawn upon, and an implicit contents object is created when a new page is created. The methods discussed below create a new canvas for writing. It has the same physical boundaries as the page itself, and should only be written to with explicit locations, as #text is not aware of non-page canvases. 4 Makes a loose content object. Output will go into this object until it is closed. This object will not appear until it is included within a page. The method will return the object reference. To aid in the conceptual grouping of modifications to a loose content object, this method will yield the opened object if a block is provided. 4 Makes the object for current content insertion the object specified by id, which is a value returned by either #open_object or #new_page. 4 Closes the currently open loose content object, preventing further writing against the object. 3 Once a loose contents object has been created, it must be added to the collection of loose objects before it can be seen in the PDF document. PDF::Writer makes it easy to write these objects to the contents of pages when the pages are created. 4 After a loose content object has been created, it will only show if it has been added to a page or page(s) with this method. Where the loose content object will be added is controlled by the where option. The object will not be added to itself. .blist {{{ :this_page will add the object just to the current page. :all_pages will add the object to the current and all following pages. :even_pages will add the object to following even pages, including the current page if it is an even page. :odd_pages will add the object to following odd pages, including the current page if it is an odd page. :all_following_pages will add the object to the next page created and all following pages. :following_even_pages will add to the next even page created and all following even pages. :following_odd_pages will add to the next odd page created and all following odd pages. .endblist }}} 4 Stops adding the specified object to pages after this page. 3 The following example is used to create the heading of this manual. .code {{{ pdf.open_object do |heading| pdf.save_state pdf.stroke_color! Color::Black pdf.stroke_style! PDF::Writer::StrokeStyle::DEFAULT s = 6 t = "PDF::Writer for Ruby ~ Manual" w = pdf.text_width(t, s) / 2.0 x = pdf.margin_x_middle y = pdf.absolute_top_margin pdf.add_text(x - w, y, t, s) x = pdf.absolute_left_margin w = pdf.absolute_right_margin y -= (pdf.font_height(s) * 1.01) pdf.line(x, y, w, y).stroke pdf.restore_state pdf.close_object pdf.add_object(heading, :next_all_pages) end .endcode }}} 2ActiveDocumentElements Hyperlinks—to locations within the document or on the Internet—can be created in any document either using the callback forms (<c:alink> and <c:ilink>) for words, or #add_link and #add_internal_link for any other location in a document. Before internal links will work, the destination to which each refers must be created with #add_destination. In fact, a PDF document will not render if there is an internal link reference to a nonexistant destination. For those familiar with HTML, this is similar to the following: .code {{{ <h1 id="title">Title of Document</h1> ... <a href="#title">Top</a> .endcode }}} 3 This method is used to create a labelled destination at this point in the document. The label is a string containing the name which will be used for links internal to the document (created by <c:ilink> or #add_internal_link). The destination style controls how many parameters are required. 4 The viewport will be opened at position (left, top) with zoom percentage. If the values are the string "null", the current parameter values are unchanged. 4 Fit the page to the viewport (horizontal and vertical). There are no parameters. 4 Fit the page horizontally to the viewport. The top of the viewport is set to top. 4 Fit the page vertically to the viewport. The left of the viewport is set to left. 4 Fits the page to the provided rectangle with corners at (left, bottom) and (right, top). 4 Fits the page to the bounding box of the page. There are no parameters. 4 Fits the page horizontally to the bounding box of the page. The top of the viewport is set to top. 4 Fits the page vertically to the bounding box of the page. The left of the viewport is set to left. 3internal_links Creates an internal link in the document, ‘label’ is the name of an target point, and the other settings are the coordinates of the enclosing rectangle of the clickable area from (x0, y0) to (x1, y1). Note that the destination markers are created using the #add_destination method described below. This the manual way of doing what <c:ilink> does. 3external_links Creates a clickable rectangular area from (x0, y0) to (x1, y1) within the current page of the document, which takes the user to the specified URI when clicked. This is the manual way of doing what <c:alink> does. .code {{{ pdf.add_link("http://rubyforge.org/projects/ruby-pdf/", pdf.left_margin, 100, pdf.left_margin + 400, 120) pdf.text("http://rubyforge.org/projects/ruby-pdf/", :font_size => 16, :justification => :centre, :left => 50) pdf.rectangle(50, 100, 400, 20).stroke .endcode }}} .eval {{{ pdf.move_pointer(30, true) pdf.move_pointer(-5) pdf.text("http://rubyforge.org/projects/ruby-pdf/", :font_size => 16, :justification => :centre, :left => 50) pdf.move_pointer(5) pdf.add_link("http://rubyforge.org/projects/ruby-pdf/", pdf.left_margin, pdf.y, pdf.left_margin + 400, pdf.y + 20) pdf.rectangle(pdf.left_margin, pdf.y, 400, 20).stroke .endeval }}} 2 PDF normally renders text by filling the path described by the font in use. PDF::Writer provides (through PDF::Writer::Graphics) three methods to change the way that text is rendered. 3<#text_render_style(style)> 3<#text_render_style!(style)> 3<#text_render_style?> The first and second methods will set the text rendering style; the second forces the style to be set in the PDF document even if it’s the same as the currently known text rendering style. The third method returns the current text rendering style. The text rendering styles are integer values zero through seven. The meaning of “fill” and “stroke” are explained in-depth . PDF::Writer does not support the use of clipping paths at this point. .blist {{{ 0: (default) Fill the text. Uses #fill_color for text rendering colour. 1: Stroke the text. 2: Fill, then stroke the text. 3: Neither fill nor stroke the text (e.g., it is invisible). 4: Fill the text and add it to the clipping path. 5: Stroke the text and add it to the clipping path. 6: Fill, then stroke the text and add it to the clipping path. 7: Add the text to the clipping path. .endblist }}} .eval {{{ pdf.move_pointer(160, true) y = pdf.y + 90 pdf.select_font "Times-Roman" pdf.fill_color Color::RGB::Grey70 pdf.stroke_color! Color::RGB::Black pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.rectangle(pdf.absolute_left_margin, y + 64, pdf.margin_width, -100).fill_stroke pdf.fill_color Color::RGB::Black (0..7).each do |style| pdf.add_text(pdf.absolute_left_margin + 20 + (60 * style), y + 48, style.to_s, 12) end (0..7).each do |style| pdf.text_render_style!(style) pdf.add_text(pdf.absolute_left_margin + 5 + (60 * style), y, "Q", 64) end .endeval }}} .newpage 1GraphicsOps Graphics primitives are provided by the module PDF::Writer::Graphics, included into PDF::Writer so that graphics primitives may be accessed from PDF document objects transparently. All drawing operations return the canvas so that drawing operations can be chained. 2 PDF graphics drawing operations are different than most drawing libraries. In most libraries, when a straight line is drawn between point A and point B, the line is drawn in the current colour, line thickness, and style. PDF drawing does not draw the line, but instead plots a path. Only when a path is painted will it be rendered in the document as a visible drawing. As an illustration, with the following code: .code {{{ pdf.move_to(70, 70).line_to(100, 100).line_to(40, 40).line_to(70, 70) .endcode }}} Nothing will be drawn until it is painted (with a stroke operation in this case): .code {{{ pdf.stroke .endcode }}} The code for this can be simplified, too. .code {{{ pdf.move_to(70, 70).line_to(100, 100).line_to(40, 40).close_stroke .endcode }}} .eval {{{ pdf.move_pointer(50, true) pdf.move_to(pdf.margin_x_middle, pdf.y - 20) pdf.line_to(pdf.margin_x_middle + 30, pdf.y + 10) pdf.line_to(pdf.margin_x_middle - 30, pdf.y - 10) pdf.close_stroke pdf.move_pointer(20, true) .endeval }}} Drawing paths may be arbitrarily long and may be comprised of several sub-paths. Paths are considered sub-paths when a new path is started. Only a paint operation finishes all drawing paths. Much of the precise behaviour will become clearer as the graphics operations are introduced over the next sections. 2DrawingStyles PDF documents keep track of two separate drawing colours at any given time: the stroke colour and the fill colour, used to render the respective paint operations (see ). Stroke operations need not be rendered with solid lines; this is controlled by the . 3 Both stroke and fill colours should be set to instances of either Color::RGB or Color::CMYK, defined and documented in the color-tools package. This helper package is briefly documented in “”. 4<#stroke_color(color)> 4<#fill_color(color)> 4<#stroke_color!(color = nil)> 4<#fill_color!(color = nil)> 4<#stroke_color?> 4<#fill_color?> These six methods set and report the colour for stroke and fill operations, respectively. The first two will only change the stroke/fill colour if it is different than the current stroke/fill colour. The second will force the current stroke/fill colour to be set or reset, unless a nil value is provided. The third returns the value of the current stroke/fill colour. 3StrokeStyle Stroke styles must be instances of the PDF::Writer::StrokeStyle class. 4StrokeStyleClass This class represents how lines will be drawn with stroke operations. Line styles are defined by width, line cap style, join method, miter limit, and dash patterns. 5 Creates the new stroke style with the specified width and options. The options correspond to the attributes described below. If a block is provided, the stroke object is yielded to it. 5 The thickness of the line in PDF units. .code {{{ s1 = PDF::Writer::StrokeStyle.new(1) s2 = PDF::Writer::StrokeStyle.new(2) s3 = PDF::Writer::StrokeStyle.new(3) s4 = PDF::Writer::StrokeStyle.new(4) s5 = PDF::Writer::StrokeStyle.new(5) .endcode }}} .eval {{{ s1 = PDF::Writer::StrokeStyle.new(1) pdf.stroke_style s1 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke s2 = PDF::Writer::StrokeStyle.new(2) pdf.stroke_style s2 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke s3 = PDF::Writer::StrokeStyle.new(3) pdf.stroke_style s3 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke s4 = PDF::Writer::StrokeStyle.new(4) pdf.stroke_style s4 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke s5 = PDF::Writer::StrokeStyle.new(5) pdf.stroke_style s5 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke pdf.move_pointer(5, true) .endeval }}} 5 The type of cap to put on the ends of the line. .blist {{{ :butt caps squre off the stroke at the endpoint of the path. There is no projection beyond the end of the path. :round caps draw a semicircular arc with a diameter equal to the line width is drawn around the endpoint and filled in. :square caps continue the stroke beyond the endpoint of the path for a distance equal to half the line width and is squared off. .endblist }}} .code {{{ s5-butt s5.cap = :butt .endcode }}} .eval {{{ s5-butt s5 = PDF::Writer::StrokeStyle.new(5, :cap => :butt) pdf.stroke_style s5 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke pdf.move_pointer(5, true) .endeval }}} .code {{{ s5-round s5.cap = :round .endcode }}} .eval {{{ s5-round s5 = PDF::Writer::StrokeStyle.new(5, :cap => :round) pdf.stroke_style s5 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke pdf.move_pointer(5, true) .endeval }}} .code {{{ s5-square s5.cap = :square .endcode }}} .eval {{{ s5-square s5 = PDF::Writer::StrokeStyle.new(5, :cap => :square) s5.cap = :square pdf.stroke_style s5 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke pdf.move_pointer(5, true) .endeval }}} If this is unspecified, the cap style is unchanged. 5 How two lines join together. .blist {{{ :miter joins indicate that the outer edges of the strokes for the two segments are extended until they meet at an angle, as in a picture frame. If the segments meet at too sharp an angle (as defined by the #miter_limit), a bevel join is used instead. :round joins draw an arc of a circle with a diameter equal to the line width is drawn around the point where the two segments meet, connecting the outer edges of the strokes for the two segments. This pie-slice shaped figure is filled in, producing a rounded corner. :bevel joins finish the two segments with butt caps and the the resulting notch beyond the ends of the segments is filled with a triangle, forming a flattened edge on the join. .endblist }}} .code {{{ s5-miter s5.join = :miter .endcode }}} .eval {{{ s5-miter s5 = PDF::Writer::StrokeStyle.new(5, :join => :miter) pdf.stroke_style s5 pdf.move_pointer(40, true) pdf.move_to(100, pdf.y).line_to(100, pdf.y + 30) pdf.line_to(400, pdf.y + 30).stroke pdf.move_pointer(5, true) .endeval }}} .code {{{ s5-round s5.join = :round .endcode }}} .eval {{{ s5-round s5 = PDF::Writer::StrokeStyle.new(5, :join => :round) pdf.stroke_style s5 pdf.move_pointer(40, true) pdf.move_to(100, pdf.y).line_to(100, pdf.y + 30) pdf.line_to(400, pdf.y + 30).stroke pdf.move_pointer(5, true) .endeval }}} .code {{{ s5-bevel s5.join = :bevel .endcode }}} .eval {{{ s5-bevel s5 = PDF::Writer::StrokeStyle.new(5, :join => :bevel) pdf.stroke_style s5 pdf.move_pointer(40, true) pdf.move_to(100, pdf.y).line_to(100, pdf.y + 30) pdf.line_to(400, pdf.y + 30).stroke pdf.move_pointer(5, true) .endeval }}} If this is unspecified, the join style is unchanged. 5 When two line segments meet and :miter joins have been specified, the miter may extend far beyond the thickness of the line stroking the path. The #miter_limit imposes a maximum ratio miter length to line width at which point the join will be converted from a miter to a bevel. Adobe points out that the ratio is directly related to the angle between the segments in user space. With [p] representing the angle at which the segments meet: .code {{{ miter_length / line_width == 1 / (sin ([p] / 2)) .endcode }}} A miter limit of 1.414 converts miters to bevels for [p] less than 90 degrees, a limit of 2.0 converts them for [p] less than 60 degrees, and a limit of 10.0 converts them for [p] less than approximately 11.5 degrees. 5 Controls the pattern of dashes and gaps used to stroke paths. This value must either be nil, or a hash with :pattern and :phase The :pattern is an array of numbers specifying the lengths (in PDF userspace units) of alternating dashes and gaps. The array is processed cyclically, so that { :pattern => [3] } represents three units on, three units off, and { :pattern => [2, 1] } represents two units on, one unit off. These are shown against a solid line for comparison. .eval {{{ :pattern sp1 = PDF::Writer::StrokeStyle.new(5, :dash => PDF::Writer::StrokeStyle::SOLID_LINE) pdf.stroke_style sp1 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke sp2 = PDF::Writer::StrokeStyle.new(5, :dash => { :pattern => [3] }) pdf.stroke_style sp2 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke sp3 = PDF::Writer::StrokeStyle.new(5, :dash => { :pattern => [2, 1] }) pdf.stroke_style sp3 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke pdf.move_pointer(5, true) .endeval }}} The :phase is an integer offset to the :pattern where the drawing of the stroke begins. This means that { :pattern => [3], :phase => 1 } represents be two units on and followed by a repeating pattern of three units off, three units on. A dash setting of { :pattern => [2, 1], :phase => 2 } repeats an off unit followed by two units on. These are shown against a solid line for comparison. .eval {{{ :phase sp1 = PDF::Writer::StrokeStyle.new(5, :dash => PDF::Writer::StrokeStyle::SOLID_LINE) pdf.stroke_style sp1 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke sp2 = PDF::Writer::StrokeStyle.new(5, :dash => { :pattern => [3], :phase => 1 }) pdf.stroke_style sp2 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke sp3 = PDF::Writer::StrokeStyle.new(5, :dash => { :pattern => [2, 1], :phase => 2 }) pdf.stroke_style sp3 pdf.move_pointer(8, true) pdf.line(100, pdf.y, 400, pdf.y).stroke pdf.move_pointer(5, true) .endeval }}} StrokeStyle#dash may be set to StrokeStyle::SOLID_LINE, returning drawing to a solid line. Dashed lines wrap around curves and corners just as solid stroked lines do, with normal cap and join handling with no consideration of the dash pattern. A path with several subpaths treats each sub-path independently; the complete dash pattern is restarted at the beginning of each sub-path. 4<#stroke_style(style)> 4<#stroke_style!(style = nil)> 4<#stroke_style?(style)> These methods set and report the style for stroke operations. The first two will only change the stroke style if it is different than the current stroke style. The second will force the current stroke style to be set or reset, unless a nil value is provided. The third returns the value of the current stroke style. The default drawing style (a one unit solid line) can be set with the constant StrokeStyle::DEFAULT. 2 PDF drawings can only derive curved images (including ellipses and circles) through a concept known as cubic Bézier curves. These draw curved lines using four control points. (A demonstration applet can be found at the Princeton computer science department). The distinctive feature of these curves is that the curve is guaranteed to be completely contained by the four sided polygon formed from the control points. The example below demonstrates a Bézier curve with its control points and three sides of the polygon marked. The curve is tangent to the line between the control points at either end of the curve. .code {{{ pdf.curve(200, pdf.y + 40, 250, pdf.y + 5, 350, pdf.y, 400, pdf.y + 45).stroke .endcode }}} .eval {{{ pdf.move_pointer(50, true) pdf.stroke_style PDF::Writer::StrokeStyle.new(2) pdf.curve(200, pdf.y + 40, 250, pdf.y + 5, 350, pdf.y, 400, pdf.y + 45).stroke pdf.stroke_style PDF::Writer::StrokeStyle.new(0.5) pdf.circle_at(200, pdf.y + 40, 5).stroke pdf.circle_at(250, pdf.y + 5, 5).stroke pdf.circle_at(350, pdf.y, 5).stroke pdf.circle_at(400, pdf.y + 45, 5).stroke stroke = PDF::Writer::StrokeStyle.new(0.5, :dash => { :pattern => [10, 15] }) pdf.stroke_style(stroke) pdf.line(200, pdf.y + 40, 250, pdf.y + 5).stroke pdf.line(250, pdf.y + 5, 350, pdf.y).stroke pdf.line(350, pdf.y, 400, pdf.y + 45).stroke .endeval }}} 2 These primitives control the drawing path that can later be painted. PDF documents have a single drawing point for drawing operations. The operators described here work with this drawing point and move the drawing point to a new position. 4<#move_to(x, y)> Moves the drawing point to (x, y). This will start a new sub-path. 4<#line_to(x, y)> Continues the current sub-path in a straight line from the drawing point to the new (x, y) coordiante. The drawing point is left at (x, y). 4<#curve_to(x0, y0, x1, y1, x2, y2)> This draws a Bézier curve with the drawing point (dpx, dpy) and (x2, y2) as the terminal points. (x0, y0) and (x1, y1) are the control points on the curve. The drawing pointer is set to (x2, y2) at the end of the curve. 4<#scurve_to(x0, y0, x1, y1)> This draws a Bézier spline with the drawing point (dpx, dpy) and (x1, y1) as the terminal points. (dpx, dpy) and (x0, y0) are the control points on the curve. The drawing pointer is set to (x1, y1) at the end of the curve. 4<#ecurve_to(x0, y0, x1, y1)> This draws a Bézier spline with the drawing point (dpx, dpy) and (x1, y1) as the terminal points. (x0, y0) and (x1, y1) are the control points on the curve. The drawing pointer is set to (x1, y1) at the end of the curve. 4<#rectangle(x, y, w, h = w)> Draws a rectangle from (x, y) to (x + w, y + h). The drawing pointer is set to (x + w, y + h) when complete. This is a basic PDF drawing primitive that continues the current drawing sub-path. 4<#close> Closes the current sub-path by appending a straight line segment from the drawing point to the starting point of the path. If the path is already closed, this is does nothing. This operator terminates the current sub-path. 2 These operators build on the drawing path primitives to provide complete shapes for drawing. These will always generate new sub-paths. 4<#line(x0, y0, x1, y1)> Draws a straight line from (x0, y0) to (x1, y1) and leaves the drawing pointer at (x1, y1). .code {{{ pdf.line(100, pdf.y - 5, 400, pdf.y + 5) .endcode }}} .eval {{{ pdf.move_pointer(20, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.line(100, pdf.y - 15, 400, pdf.y - 5).stroke .endeval }}} 4<#curve(x0, y0, x1, y1, x2, y2, x3, y3)> This draws a Bézier curve with (x0, x1) and (x3, y3) as the terminal points. (x1, y1) and (x2, y2) are the control points on the curve. The drawing pointer is set to (x3, y3) at the end of the curve. .code {{{ pdf.curve(100, pdf.y - 5, 200, pdf.y + 5, 300, pdf.y - 5, 400, pdf.y + 5 .endcode }}} .eval {{{ pdf.move_pointer(20, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.curve(100, pdf.y - 15, 200, pdf.y - 5, 300, pdf.y - 15, 400, pdf.y - 5) pdf.stroke .endeval }}} 4<#scurve(x0, y0, x1, y1, x2, y2)> This draws a Bézier spline with (x0, y0) and (x2, y2) as the terminal points. (x0, y0) and (x1, y1) are the control points on the curve. The drawing pointer is set to (x2, y2) at the end of the curve. .code {{{ pdf.scurve(100, pdf.y - 5, 300, pdf.y + 5, 400, pdf.y - 5) .endcode }}} .eval {{{ pdf.move_pointer(20, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.scurve(100, pdf.y - 15, 300, pdf.y - 5, 400, pdf.y - 15).stroke .endeval }}} 4<#ecurve(x0, y0, x1, y1, x2, y2)> This draws a Bézier spline with (x0, y0) and (x2, y2) as the terminal points. (x1, y1) and (x2, y2) are the control points on the curve. The drawing pointer is set to (x2, y2) at the end of the curve. .code {{{ pdf.ecurve(100, pdf.y - 5, 200, pdf.y + 5, 400, pdf.y - 5) .endcode }}} .eval {{{ pdf.move_pointer(20, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.ecurve(100, pdf.y - 15, 200, pdf.y - 5, 400, pdf.y - 15) pdf.stroke .endeval }}} 4<#circle_at(x, y, r)> Draws a circle of radius r with centre point at (x, y). The drawing pointer is moved to (x, y) when finished. .code {{{ pdf.circle_at(250, pdf.y, 10) .endcode }}} .eval {{{ pdf.move_pointer(30, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.circle_at(250, pdf.y + 15, 10) pdf.stroke .endeval }}} 4<#ellipse_at(x, y, r1, r2 = r1)> Draws an ellipse of horizontal radius r1 and vertical radius r2 with centre point at (x, y). The drawing pointer is moved to (x, y) when finished. .code {{{ pdf.ellipse_at(250, pdf.y, 20, 10) .endcode }}} .eval {{{ pdf.move_pointer(30, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.ellipse_at(250, pdf.y + 15, 20, 10) pdf.stroke .endeval }}} 4<#ellipse2_at(x, y, r1, r2 = r1, start = 0, stop = 360, segments = 8)> Draws an ellipse with centre point at (x, y). The horizontal radius is r1 and the vertical radius is r2. A partial ellipse may be drawn by specifying the starting and finishing angles in degrees. The ellipse is drawn in segments composed of cubic Bézier curves. It is not recommended that this be set to a value smalelr than 4, and any value less than 2 will be treated as 2. The drawing pointer is moved to (x, y) when finished. This ellipse is more “natural” at smaller sizes. .code {{{ pdf.ellipse2_at(250, pdf.y, 20, 10) .endcode }}} .eval {{{ pdf.move_pointer(30, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.ellipse2_at(250, pdf.y + 15, 20, 10).stroke .endeval }}} .code {{{ pdf.ellipse2_at(250, pdf.y, 20, 10, 135, -45) .endcode }}} .eval {{{ pdf.move_pointer(30, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.ellipse2_at(250, pdf.y + 15, 20, 10, 135, -45).stroke .endeval }}} .code {{{ ss = PDF::Writer::StrokeStyle.new(2) ss.cap = :round pdf.stroke_style ss pdf.fill_color! Color::RGB::BlueViolet pdf.translate_axis(300, pdf.y + 25) pdf.circle_at(0, 0, 38).fill pdf.rotate_axis(10) pdf.stroke_color! Color::RGB::Grey30 pdf.ellipse2_at(0, 0, 81, 20, 65.5, -245.5).stroke pdf.ellipse2_at(0, 0, 85, 22, 67.5, -247.5).stroke pdf.ellipse2_at(0, 0, 89, 25, 70.5, -250.5).stroke pdf.restore_state .endcode }}} .eval {{{ pdf.move_pointer(60, true) ss = PDF::Writer::StrokeStyle.new(2) ss.cap = :round pdf.stroke_style ss pdf.fill_color! Color::RGB::BlueViolet pdf.translate_axis(300, pdf.y + 25) pdf.circle_at(0, 0, 38).fill pdf.rotate_axis(10) pdf.stroke_color! Color::RGB::Grey30 pdf.ellipse2_at(0, 0, 81, 20, 65.5, -245.5).stroke pdf.ellipse2_at(0, 0, 85, 22, 67.5, -247.5).stroke pdf.ellipse2_at(0, 0, 89, 25, 70.5, -250.5).stroke .endeval }}} 4<#segment_at(x, y, r1, r2 = r1, start = 0, stop = 359.99, segments = 8)> Draws an ellipse segment. This is a closed partial ellipse with lines from the starting point to the centre point and the ending point and the centre point. The drawing pointer is moved to (x, y) when finished. .code {{{ pdf.segment_at(250, pdf.y, 20, 10, 45, -45) .endcode }}} .eval {{{ pdf.move_pointer(30, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.segment_at(250, pdf.y + 15, 20, 10, 45, -45) pdf.stroke .endeval }}} 4<#polygon(points)> Draws a polygon using points. This is an array of PDF::Writer::PolygonPoint objects or an array that can be converted to an array of PolygonPoint objects. A PolygonPoint is a simple object that contains (x, y) coordinates for the point and the way that the line will be connected to the previous point. The connnector may be either :curve, :scurve, :ecurve, or :line. The connector is ignored on the first point provided to the polygon; that is the starting point. The drawing point is left at the last (x, y) point position. 5<:curve> A :curve connector indicates that there are three points following this point, to be provided as parameters to #curve_to. .code {{{ points = [ [ 10, 10 ], # starting point [ 20, 20, :curve ], # first control point [ 30, 30 ], # second control point [ 40, 40 ], # ending point of the curve. ] .endcode }}} 5<:scurve> An :scurve connector indicates that there are two points following this point, to be provided as parameters to #scurve_to. .code {{{ points = [ [ 10, 10 ], # starting point and first control point [ 20, 20, :scurve ], # second control point [ 30, 30 ], # ending point of the curve. ] .endcode }}} 5<:ecurve> An :ecurve connector indicates that there are two points following this point, to be provided as parameters to #ecurve_to. .code {{{ points = [ [ 10, 10 ], # starting point [ 20, 20, :ecurve ], # first control point [ 30, 30 ], # ending point and second control point ] .endcode }}} 5<:line> A :line connector draws a line between the previous point and this point. .code {{{ points = [ [ 10, 10 ], # starting point [ 20, 20, :line ], # ending point ] .endcode }}} 5 All of these examples use :line connectors. .code {{{ pdata = [[200, 10], [400, 20], [300, 50], [150, 40]] pdf.polygon(pdata) .endcode }}} .eval {{{ pdata = [[200, 10], [400, 20], [300, 50], [150, 40]] pdf.move_pointer(60, true) pdata.collect! { |e| [e[0], e[1] + pdf.y] } pdf.polygon(pdata).stroke .endeval }}} .code {{{ pdf.polygon(pdata).fill .endcode }}} .eval {{{ pdata = [[200, 10], [400, 20], [300, 50], [150, 40]] pdf.move_pointer(60, true) pdata.collect! { |e| [e[0], e[1] + pdf.y] } pdf.polygon(pdata).fill .endeval }}} .code {{{ pdf.fill_color(Color::RGB.from_fraction(0.9, 0.8, 0.7)) pdf.polygon(pdata).fill .endcode }}} .eval {{{ pdata = [[200, 10], [400, 20], [300, 50], [150, 40]] pdf.move_pointer(60, true) pdata.collect! { |e| [e[0], e[1] + pdf.y] } pdf.fill_color(Color::RGB.from_fraction(0.9, 0.8, 0.7)) pdf.polygon(pdata).fill .endeval }}} 4<#rounded_rectangle(x, y, w, h, r)> Draws a rectangle with rounded corners. The drawing point is left at the (x + w, y + h). The radius should be smaller than the width and height. .code {{{ pdf.rounded_rectangle(100, pdf.y, 300, 30, 5) .endcode }}} .eval {{{ pdf.move_pointer(40, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.rounded_rectangle(100, pdf.y + 35, 100, 30, 10) pdf.stroke pdf.move_pointer(5, true) .endeval }}} 4<#star(cx, cy, length, rays = 5)> Draws a star centered at (cx, cy) with the specified number of rays, where each ray is length units long. Stars with an odd number of rays should have the top ray pointing toward the top of the document. If the number of rays is less than four, it will be converted to four. .code {{{ pdf.star(166, pdf.y, 20, 4).stroke pdf.star(232, pdf.y, 20, 5).stroke pdf.star(298, pdf.y, 20, 6).stroke pdf.star(364, pdf.y, 20, 7).stroke pdf.star(430, pdf.y, 20, 8).stroke .endcode }}} .eval {{{ pdf.move_pointer(40, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black pdf.star(166, pdf.y + 15, 20, 4).stroke pdf.star(232, pdf.y + 15, 20, 5).stroke pdf.star(298, pdf.y + 15, 20, 6).stroke pdf.star(364, pdf.y + 15, 20, 7).stroke pdf.star(430, pdf.y + 15, 20, 8).stroke pdf.move_pointer(5, true) .endeval }}} 2PaintingPaths Drawing paths become visible in PDF documents when they are painted. In painting a path, the path may be stroked or filled. 3 Painting a path with a stroke draws a line along the current path. The line follows each segment in the path, centered on the segment with sides parallel to it. Each segment of the line uses the current line width and dash pattern. Lines that meet use the current join style; ends of lines unconnected to other lines use the current line cap style. When two unconnected segments meet or intersect in the same space, they will not be drawn as a closed segment; an explicit #close operator is recommended in this case. If paths are manually closed, according to the Adobe PDF Reference Manual version 1.6, the result may be a “messy corner, because line caps are applied instead of a line join.” There is the special case of single-point paths; when a path is either a closed path that is exactly one point in size or of two or more points at the same coordinates, the #stroke operator only paints the path if :round line caps are specified, producing a filled circle centered at the single point. This is considered a “degenerate” sub-path. An open degenerate sub-path is not drawn. 3 Fill paint operations colour the entire region enclosed by the current path with the current fill colour. If there are several disconnected subpaths, open subpaths are implicitly closed and the overall enclosed area is painted—the space is “joined” before painting. Degenerate subpaths (the same as described above) will result in the painting of the single device pixel lying under that point—this is device dependent and not useful. An open degenerate sub-path is not drawn. Filling simple paths is intuitively clear: parts of the document enclosed in the path will be painted in the current fill colour. Complex paths—those that intersect themselves or have one or more sub-paths enclosing other sub-paths—require additional considerations for knowing what portions of the canvas are inside a path. PDF offers two different rules: nonzero winding number and the even-odd. 4 This is the standard fill behaviour. In this rule, a given point is inside a path by conceptually drawing a line from the point to beyond the outer edge of the path. Using an initial value of zero, at every place where the path crosses the ray from left to right, one will be added to the value; where it crosses from right to left, one will be subtracted from the value. If the resulting value is zero, the point is outside of the path. In all other cases, the path is inside. This can be expressed in Ruby as: .code {{{ crossings.inject(0) { |ii, xd| xd.left_right? ? ii + 1 : ii - 1 } != 0 .endcode }}} For the purposes of applying this rule, if a ray concides with or is tangent to a path segment, a different ray is chosen. Consider the two six-pointed stars below. The first is drawn so that all paths head in the same direction: .code {{{ pdf.move_to(x0, y1).line_to(x2, y1).line_to(x1, y3).close pdf.move_to(x1, y0).line_to(x2, y2).line_to(x0, y2).close .endcode }}} The second is drawn differently: .code {{{ pdf.move_to(x0, y1).line_to(x2, y1).line_to(x1, y3).close pdf.move_to(x0, y2).line_to(x2, y2).line_to(x1, y0).close .endcode }}} In the first, the figure is solid. In the second, because of the nonzero winding number fill rule, it is hollow. .eval {{{ pdf.move_pointer(60, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black y0 = pdf.y + 50 # top y1 = y0 - 10 # top-middle y2 = y1 - 20 # bottom-middle y3 = y2 - 10 # bottom x0 = 140 x1 = 160 x2 = 180 pdf.move_to(x0, y1).line_to(x2, y1).line_to(x1, y3).close pdf.move_to(x1, y0).line_to(x2, y2).line_to(x0, y2).close pdf.fill x0 += 100 x1 += 100 x2 += 100 pdf.move_to(x0, y1).line_to(x2, y1).line_to(x1, y3).close pdf.move_to(x0, y2).line_to(x2, y2).line_to(x1, y0).close pdf.fill .endeval }}} 4 The even-odd fill rule uses the same imaginary ray concept as the nonzero winding number. Instead of depending on the direction of the intersections of path with the ray, it depends on the total number of intersections. If that total is odd, the point is inside the path for painting purposes. If the total is even, the point is outside the path. Shown below are the same two figures used in the nonzero winding number rule, but this time they are filled using the even-odd rule. They appear the same. .eval {{{ pdf.move_pointer(60, true) pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT pdf.stroke_color Color::RGB::Black y0 = pdf.y + 50 # top y1 = y0 - 10 # top-middle y2 = y1 - 20 # bottom-middle y3 = y2 - 10 # bottom x0 = 140 x1 = 160 x2 = 180 pdf.move_to(x0, y1).line_to(x2, y1).line_to(x1, y3).close pdf.move_to(x1, y0).line_to(x2, y2).line_to(x0, y2).close pdf.fill(:even_odd) x0 += 100 x1 += 100 x2 += 100 pdf.move_to(x0, y1).line_to(x2, y1).line_to(x1, y3).close pdf.move_to(x0, y2).line_to(x2, y2).line_to(x1, y0).close pdf.fill(:even_odd) .endeval }}} 4 5<#stroke> Terminate the path object and draw it with the current stroke style. 5<#close_stroke> Close the current path by appending a straight line segment from the drawing point to the starting point of the path and then stroke it. This is the same as “pdf.close.stroke”. 5<#fill(rule = nil)> Fills the path according to the rules noted above, using the nonzero winding number rule unless the rule parameter has the value :even_odd. Uses the current fill colour. 5<#close_fill(rule = nil)> Close the current path by appending a straight line segment from the drawing point to the starting point of the path and then fill it. This is the same as “pdf.close.fill”. 5<#fill_stroke(rule = nil)> Fills and strokes the path. This is the same as constructing two identical path objects and calling #fill on the first and #stroke on the second. Paths filled and stroked in this manner are treated as if they were one object for PDF transparency purposes (the PDF transparency model is not yet supported by PDF::Writer). 5<#close_fill_stroke(rule = nil)> Closes (as per #close), fills (as per #fill), and strokes (as per #stroke) the drawing path. As with #fill_stroke, it constructs a single object that acts like two paths. 2PDFImages PDF::Writer supports the insertion of images into documents. These images must be either JPEG (either RGB or CMYK colour spaces) or PNG format; other format images must be converted to JPEG or PNG before they can be used in a document. Images are inserted into PDF documents as one pixel per PDF userspace unit, making a 320×240 image approximately 4½”×3¼” (113mm×85mm) in size. 3<#add_image(image, x, y, width = nil, height = nil, image_info = nil, link = nil)> Adds an image to the PDF document. The image must be a String that represents the binary contents of a PNG or JPEG file or it must be a PDF image that has previously been inserted by one of the image insertion methods (including this one). The loaded image will be placed with its lower left-hand corner at (x, y) on the current page. The size of the image will be determined by the width and height parameters. If neither width nor height are provided, the image will be inserted at its “natural” size. If width is specified, but height is not specified, then the image will be scaled according to the width of the image. If height is specified, but width is not, the image will be scaled according to the height of the image. In both cases, the aspect ratio—the ratio of width to height—will be maintained. If both width and height are specified, the set values will be used. If the width and/or height values are negative, the image will be flipped horizontally or vertically, as appropriate. The image_info parameter must either be unspecified, nil, or a valid instance of PDF::Writer::Graphics::ImageInfo (see “”). The link parameter allows the image to be made as a clickable link to internal or external targets. It must be specified as a hash with two options: .blist {{{ :type is the type of the link, either :internal or :external. This affects the interpretation of :target. :target is the destination of the link. For :type => :internal links, this is an internal destination. For :type => :external links, this is an URI. See and for more information. .endblist }}} The image object will be returned so that it can be used again in the future. 3<#add_image_from_file(image, x, y, width = nil, height = nil, link = nil)> Adds an image to the PDF document from a file. The image parameter may be an IO-like object (it must respond to #read and return the full image data in a single read) or a filename. If open-uri is active, the filename may also be an URI to a remote image. In all other ways, it behaves just like #add_image. 3<#image(image, options = {})> This third method for inserting images into a PDF document holds much the same relationship to #add_image and #add_image_from_file as #text holds to the text insertion methods #add_text and #add_text_wrap. This method will insert an image in the document positioned vertically relative to the current internal writing pointer. The horizontal position and image size are controlled by the options passed to the method. If the image is taller than the available space on the page and the image is not to be resized to the available space (see below), a new page will be created and the image will be inserted there. 4<:pad> The image will be inserted into the document with this number of blank userspace units on all sides. The default is 5 units. 4<:width> The desired width of the image in userspace units. The image will be resized to this width using the aspect ratio of the image. The image will not be allowed to be larger than the amount of space available for the image as controlled by the document margins and the padding. So, if a page is 400 units wide with 50 unit margins, that leaves an available size of 300 units. With the default padding of 5 units, the inserted picture will be no larger than 290 units wide, even if :width is set larger. 4<:resize> How to automatically resize the image. This may be either :width, :full, or a numeric value. .blist {{{ :resize => :width will resize the image to be as wide as the writing area. The image height will be resized in accordance with the aspect ratio of the image. If the resized image is taller than will fit in the remaining space on the page, a new page will be created and the image will be inserted at the top of the next page. :resize => :full will resize the image to be as wide as the writing area. The image height will be resized in accordinace with the aspect ratio of the image. If the resized image is taller than will fit in the remaining space on the page, the image will be resized again to be as tall as the remaining space. The width will be correspondingly shrunk. :resize => number will resize the image by a factor of the provided number. :resize => 2 will double the size of the image and :resize > 0.5 will halve the size of the image. If the resized image is wider than the maximum width, it will be resized again to be no wider than that, with a proportional height. .endblist }}} This option is incompatible with the :width option. The behaviour when both are specified is undefined. 4<:justification> The horizontal placement of the image. It may be :center (the centre of the image is placed in the centre of the margins and padding space), :right (the right edge of the image is flush with the right margin and padding space), or :left (the left edge of the image is flush with the left margin and padding space). The default justification is :left. 4<:border> The image may optionally be drawn with a border. If this parameter is true, the default border will be drawn, which is a 50% grey solid line border. The parameter may also be a hash with two values. .blist {{{ :color specifies the colour of the border. :style specifies the stroke style of the border. .endblist }}} 4<:link> The image may be made a clickable link to internal or external targets. This option must be specified as a hash with two options: .blist {{{ :type is the type of the link, either :internal or :external. This affects the interpretation of :target. :target is the destination of the link. For :type => :internal links, this is an internal destination. For :type => :external links, this is an URI. See and for more information. .endblist }}} 3 Because #text and #image comprise a simple layout engine, the limitations of these methods must be understood. The image will be inserted and text will be inserted after it. There is no option to place the image and then wrap text around it as one might with OpenOffice or a proper destkop publishing system. The one example of anything similar in this manual (page ) was done manually. 2CTMatrix As described earlier in “,” the standard PDF coordinate space has the base coordinate (0, 0) in the lower left-hand corner of the canvas. PDF::Writer’s layout engine depends on this behaviour, but the axis can be transformed in several ways. It can be translated, rotated, scaled, and skewed. Axis transformations are cumulative (matrix multiplicative; see the description of #transform_matrix for a full explanation) within a graphics state stack level (see the next section). It is recommended that axis transformations are performed within a #save_state/#restore_state combination. 3 Translates the coordinate space so that (x, y) under the old coordinate space is now (0, 0). The direction of the old coordinate space is unchanged (y values are increasing). The example code below will make the middle of the page the (0, 0) position. .code {{{ pdf.translate_axis(pdf.margin_x_middle, pdf.margin_y_middle) .endcode }}} 3 The entire coordinate space is rotated by the specified angle in a counter-clockwise direction. NOTE: As of PDF::Writer 1.1.0, angle rotations are now counter-clockwise, not clockwise as in earlier versions. This is a necessary incompatible change to make transformations more compatible with other vector graphics systems. 3 Changes the size of PDF userspace units. This changes the scale of all operations in PDF documents. With #scale_axis(0.5, 1), the x-axis is modified so that PDF userspace units are 1/144” in width, instead of the normal 1/72” width. Note that PDF::Writer takes no notice of scale changes for purposes of margins. If the scale value is negative, the coordinate axis is reversed and graphics or text will be drawn in reverse. 3 Rotates the x and y axes independently of one another, creating a skewed appearance. 3 Transforms the coordinate axis with the transformation vector interpreted as a coordinate transformation matrix: .eval {{{ def bracket(pdf, x, y, width, height, size) y -= (height / 1.75) tw = pdf.text_width("[", size * 4) pdf.add_text(x - tw, y, "[", size * 4) tw = pdf.text_width("]", size * 3) pdf.add_text(x + width - tw, y, "]", size * 4) x + width + tw end rowh = pdf.font_height(14) pdf.move_pointer(rowh * 5, true) mw = pdf.text_width("MMMM", 14) _x = pdf.margin_x_middle - (mw / 2.0) _y = pdf.y + (rowh * 3) pdf.add_text(_x, _y + rowh, "a c e", 14) pdf.add_text(_x, _y, "b d f", 14) pdf.add_text(_x, _y - rowh, "0 0 1", 14) bracket(pdf, _x, _y, mw, rowh, 14) pdf.move_pointer(rowh) .endeval }}} The matrix transforms the new coordinates (x1, y1) to the old coordinates (x0, y0) through the dot product: .eval {{{ def bracket(pdf, x, y, width, height, size) y -= (height / 1.75) tw = pdf.text_width("[", size * 4) pdf.add_text(x - tw, y, "[", size * 4) tw = pdf.text_width("]", size * 3) pdf.add_text(x + width - tw, y, "]", size * 4) x + width + tw end rowh = pdf.font_height(14) mtxw = pdf.text_width("MM", 14) pdf.move_pointer(rowh * 5, true) _y = pdf.y + (rowh * 3) _x = 100 pdf.add_text(_x, _y + rowh, "x0", 14) pdf.add_text(_x, _y, "y0", 14) pdf.add_text(_x, _y - rowh, " 1", 14) _x = bracket(pdf, _x, _y, mtxw, rowh, 14) pdf.add_text(_x, _y - (rowh / 1.5), '=', 42) mtxw = pdf.text_width("MMMM", 14) _x += mtxw pdf.add_text(_x, _y + rowh, "a c e", 14) pdf.add_text(_x, _y, "b d f", 14) pdf.add_text(_x, _y - rowh, "0 0 1", 14) _x = bracket(pdf, _x, _y, mtxw, rowh, 14) pdf.circle_at(_x - 3, _y + 3, 3).fill mtxw = pdf.text_width("MM", 14) _x += mtxw pdf.add_text(_x, _y + rowh, "x1", 14) pdf.add_text(_x, _y, "y1", 14) pdf.add_text(_x, _y - rowh, " 1", 14) bracket(pdf, _x, _y, mtxw, rowh, 14) pdf.move_pointer(rowh) .endeval }}} In practice, the six variables (a—f) be represented as a six-element vector: [ a b c d e f ]. .blist {{{ Axis translation uses [ 1 0 0 1 x y ] where x and y are the new (0,0) coordinates in the old axis system. Scaling uses [ sx 0 0 sy 0 0 ] where sx and sy are the scaling factors. Rotation uses [ cos(a) sin(a) -sin(a) cos(a) 0 0 ] where a is the angle, measured in radians. X axis skewing uses [ 1 0 tan(a) 1 0 0 ] where a is the angle, measured in radians. Y axis skewing uses [ 1 tan(a) 0 1 0 0 ] where a is the angle, measured in radians. .endblist }}} 2 PDF allows for multiple independent levels of graphics state to be saved in a last-in first-out stack. Graphics states may not remain open over page boundaries, and must be balanced; the use of the two methods below will ensure that this is managed automatically. 3 This operation saves the current graphics state on a stack. This stack keeps various parameters about the graphics state. .blist {{{ The stroke style (line width, line cap style, line join style, miter limit, and dash pattern), page . The coordinate axis transformation matrix, page . The colour rendering intent (not yet supported by PDF::Writer). The flatness tolerance (not yet supported by PDF::Writer). Extended graphics state dictionaries (not yet supported by PDF::Writer). .endblist }}} 3 Restores the current graphics state. 2ImageInfo This class obtains metadata information about an image. It is a modified version of ImageSize by Keisuke Minami, which can be found at http://www.rubycgi.org/tools/index.en.htm. It is available under the standard PDF::Writer licence but this class is also available under the GNU General Public Licence, version 2 or later. It supports GIF (GIF87a and GIF89a), PNG (Portable Network Graphics), JPEG, BMP (Windows or OS/2 Bitmaps), PPM, PBM, PGM, TIFF, XBM (X Bitmap), XPM (X Pixmap), PSD (PhotoShop), PCX, and SWF (Flash) image formats. 3 Creates the ImageInfo object from the provided image data. If format is specified, the image will be treated as the desired format. Otherwise, it will be automatically discovered (this is preferred). 3 Returns the format of the image. 3 3 Returns the height and width of the image. 3 Returns the colour bit depth of the image, if supported. 3 Returns the number of colour channels, if supported. 3 Returns other information the image may know about itself. .newpage 1WriterDocOps There are several operations for manipulating the generated document as a whole or metadata about the document. 2 These operations act on, or return information about, the entire document. 3 Returns the number of PDF objects in the document. 3 Indicates or sets document compression. If the value is true, the document will be compressed on writing using the “deflate” compression method. If the ‘zlib’ library cannot be loaded, compression settings will be ignored. NOTE: This value should be set as early as possible during the document generation, or only some sections of the PDF will be compressed. 3 3 The #trim_box and the #media_box are both expressed in default (unscaled) user space units. The #media_box defines the boundaries of the physical medium on which the page is intended to be displayed or printed. As of this version, PDF::writer supports only one media box for the e