Importing multiple pages into PDFLib

So, as I’ve stated before, I’m creating an application that imports a large PDF into PDFlib so that i can populate textfields on each page from a database table.

PDFlib essentially imports one page at a time to work with. So here’s code to import a multipage PDF and populate the fields for each page.

def populate_pdf
  #setup some defaults
  infile = "alaska_pdflib.pdf"
  searchpath = "../state_doc_originals"

  #this data should pull from a database, the keys of the hash must be the same as the block names on the pdf
  data = {
    "last_name" => "Constant",
    "first_name" => "Nate",
    "middle_name" => "Andrew",
    "dob_m" => "09",
    "dob_y"=>"1980",
    "dob_d" => "18",
    "city_of_birth" => "Omaha",
    "state_of_birth" => "Nebraska",
    "gender_male" => "8"
  }
  p = PDFlib.new
  if (p.begin_document("", "compatibility = 1.6") == -1)
    logger.info "Error: " + p.get_errmsg()
  end

  # Set the search path for fonts and PDF files
  p.set_parameter("SearchPath", searchpath)
  p.set_parameter("pdiwarning", "true")
  p.set_info("Creator", "businesscard.rb")
  p.set_info("Author", "QuickDocData")
  p.set_info("Title", "Alaska State License Application")

  #open the document
  blockcontainer = p.open_pdi(infile, "", 0)
  if (blockcontainer == -1)
    logger.info "Error: " + p.get_errmsg()
  end
  regularfont = p.load_font("Arial", "winansi", "")
  checkbox_font = p.load_font("ZapfDingbats","builtin","")

  #loop through all pages and put data in the blocks
  number_of_pages = p.get_pdi_value("/Root/Pages/Count",blockcontainer,-1,0)
  1.upto(number_of_pages){|page_num|
    #get current page
    page = p.open_pdi_page(blockcontainer, page_num, "")
    if (page == -1)
        logger.info "Error: " + p.get_errmsg()
    end
    #get all blocks for the page and put them in an array to use later
    num_of_blocks_on_page = p.get_pdi_value("vdp/blockcount",blockcontainer,page,0)
    cur_blocks = Array.new

    0.upto(num_of_blocks_on_page-1){ |i|
      cur_blocks.push( p.get_pdi_parameter("vdp/Blocks[#{i}]/Name", blockcontainer, page, 0))
      #logger.info p.get_pdi_parameter("vdp/Blocks[#{i}]/Name", blockcontainer, page, 0)
    }

    #set page size
    p.begin_page_ext(595, 842, "topdown")
    # This will adjust the page size to the block container's size.
    p.fit_pdi_page(page, 0, 842, "")
    # Fill all text blocks with dynamic data

    cur_blocks.each { |key, value|
      use_font = p.get_pdi_parameter("vdp/Blocks/#{key}/fontname", blockcontainer, page, 0)
      if use_font == "ZapfDingbats"#is this block a checkbox?
        font_string = "font #{checkbox_font.to_s} embedding encoding=builtin"
      else
        font_string ="font #{regularfont.to_s} embedding encoding=winansi"
      end
      if (p.fill_textblock(page, key, data[key].to_s,font_string) == -1)
        logger.info "Warning: " + p.get_errmsg
      end
    }
    p.end_page_ext("")
    p.close_pdi_page(page)
  }#end looping through all of the pages

  p.close_pdi(blockcontainer)
  p.end_document("")
  p.get_buffer()
end

About this entry