Class: CombinePDF::PDFWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/combine_pdf/combine_pdf_basic_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(media_box = [0.0, 0.0, 612.0, 792.0]) ⇒ PDFWriter



12
13
14
15
# File 'lib/combine_pdf/combine_pdf_basic_writer.rb', line 12

def initialize(media_box = [0.0, 0.0, 612.0, 792.0])
  @content_stream = {}
  @media_box = media_box
end

Instance Method Details

#add_content_to_pages(pages = [], location = :above) ⇒ Object

add_content_to_pages(pages = [], location = :above) pages - a page hash or an array of pages location - :above to place content over existing content or :below to place content under existing content



75
76
77
78
79
80
81
82
# File 'lib/combine_pdf/combine_pdf_basic_writer.rb', line 75

def add_content_to_pages(pages = [], location = :above)
  if pages.is_a?(Array)
    pages.each {|p| add_content_to_pages p, location}
  elsif pages.is_a?(Hash)
    #####
    ##add content stream to page
  end
end

#add_text_box(text, args = {}) ⇒ Object

textbox

  • font_name: :font_name

The PostScript names of 14 Type 1 fonts, known as the standard 14 fonts, are as follows: Times-Roman, Helvetica, Courier, Symbol, Times-Bold, Helvetica-Bold, Courier-Bold, ZapfDingbats, Times-Italic, Helvetica- Oblique, Courier-Oblique, Times-BoldItalic, Helvetica-BoldOblique, Courier-BoldOblique

  • text_color: [R, G, B]

an array with three floats, each in a value between 0 to 1. First value is Red, second Green and last is Blue (RGB color system)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/combine_pdf/combine_pdf_basic_writer.rb', line 25

def add_text_box(text, args = {})
  options = {
    text_alignment: :center,
    text_color: [1,1,1],
    # text_stroke: nil,
    font_name: :Helvetica,
    font_type: :Type1,
    font_object: nil,
    font_size: 12,
    border_color: nil,
    border_width: nil,
    border_radius: nil,
    background_color: nil,
    opacity: 1,
    x: 0,
    y: 0,
    length: -1,
    height: -1,
  }
  # create font object
  font_object = { Type: :Font, Subtype: options[:font_type], BaseFont: options[:font_name]}
  if options[:font_object].is_a?(Hash) && options[:font_object][:indirect_reference_id] && options[:font_object][:indirect_generation_number] && (options[:font_object][:is_reference_only] != true)
    font_object = {is_reference_only: true, referenced_object: font_object}
  end

  # create resources object
  font_name = ("MyFont" + rand(99) ).to_sym
  resources_object = {Resources: {Font: { font_name => font_object }     }    }
  # create box stream

  # reset x,y by text alignment - x,y are calculated from the buttom left
  # each unit (1) is 1/72 Inch
  x = options[:x]
  y = options[:y]
  # create text stream
  text_stream = ""
  text_stream << "BT\n" # the Begine Text marker     
  text_stream << PDFOperations._format_name_to_pdf(font_name) # Set font name
  text_stream << " #{options[:font_size].to_f} Tf\n" # set font size and add font operator
  text_stream << "#{options[:text_color][0]} #{options[:text_color][0]} #{options[:text_color][0]} rg\n" # sets the color state
  text_stream << " #{options[:opacity].to_f} ca\n" # set opacity (alpha) for graphic state.
  text_stream << "#{x} #{y} Td\n" # set location for text object
  text_stream << PDFOperations._format_string_to_pdf(text) # insert the string in PDF format
  text_stream << " Tj\n ET\n" # the Text object operator and the End Text marker
end

#make_into_pageObject

make_into_page() takes no arguments and returns the contents stream within a page (to be added as an indipendent page to the PDF object)



86
87
88
# File 'lib/combine_pdf/combine_pdf_basic_writer.rb', line 86

def make_into_page
  {Type: :Page, }
end

#to_pdf(file_name = nil) ⇒ Object

to_pdf() prints out the content stream as raw PDF file_name - the name of the file to which to save the data (will be overwritten). if file_name is given, save to file.



95
96
97
98
99
100
101
102
103
# File 'lib/combine_pdf/combine_pdf_basic_writer.rb', line 95

def to_pdf( file_name = nil)
  pdf = PDF.new
  pdf << make_into_page
  if file_name
    pdf.save file_name
  else
    pdf.to_pdf
  end
end