Module: Pdfs2Pdf

Defined in:
lib/pdfs2pdf/cli.rb,
lib/pdfs2pdf/version.rb,
lib/pdfs2pdf/pdfs2pdf.rb,
lib/pdfs2pdf/configuration.rb,
lib/pdfs2pdf/config/pdfs2pdf.rb

Defined Under Namespace

Classes: CLI, Configuration

Constant Summary collapse

VERSION =
"0.2.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Configure Pdfs2Pdf someplace sensible, like config/initializers/pdfs2pdf.rb

Pdfs2pdf.configure do |config|

# set the path to wkhtmltopdf
config.gs_binary = '/usr/bin/gs'

end



41
42
43
# File 'lib/pdfs2pdf/configuration.rb', line 41

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



45
46
47
# File 'lib/pdfs2pdf/configuration.rb', line 45

def configure
  yield(configuration)
end

.create_pdfmarks(pdf_files, pdfmarks_file = "pdfmarks", base_dir) ⇒ Object

Create the ‘pdfmarks’ file for use with ‘gs’ utility

def create_pdfmarks(pdf_files, pdfmarks_file = “pdfmarks”, base_dir = Dir.pwd)

Parameters:

  • pdf_files (Array<String>)

    the input file list (pdf)

  • output_file (String)

    the output filename default to ‘pdfmarks’

  • base_dir (String)

    the base directory



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pdfs2pdf/pdfs2pdf.rb', line 9

def create_pdfmarks(pdf_files, pdfmarks_file = "pdfmarks", base_dir)
  FileUtils.chdir(base_dir)
  File.open(pdfmarks_file, "w") do |out_file|
    out_file.write(Pdfs2Pdf.configuration.pdfmarks_meta)
    current_page = 1
    pdf_files.each do |pdf_file|
      out_file.write "[ /Page #{current_page} /Title (#{pdf_file}) /OUT pdfmark\n"
      current_page += page_count(pdf_file)
    end
  end
end

.merge_pdfs(list, pdfmarks = "pdfmarks", output_file = "pdfs2pdf_output.pdf") ⇒ Object

Merge/combine list of pdf files using Ghostscript’s ‘gs` command

Parameters:

  • list (Array<String>)

    input file list

  • pdfmarks (String) (defaults to: "pdfmarks")

    the pdfmarks file default to ‘pdfmarks’

  • output_file (String) (defaults to: "pdfs2pdf_output.pdf")

    the output pdf file



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pdfs2pdf/pdfs2pdf.rb', line 26

def merge_pdfs(list, pdfmarks = "pdfmarks", output_file = "pdfs2pdf_output.pdf")
  paper_size = Pdfs2Pdf.configuration.default_options[:paper_size]
  gs_binary  = Pdfs2Pdf.configuration.gs_binary
  _stdin, _stderr, status = Open3.capture3(
    gs_binary,
    "-q",
    "-dNOPAUSE",
    "-dBATCH",
    "-sDEVICE=pdfwrite",
    "-sPAPERSIZE=#{paper_size}",
    "-sOutputFile=#{output_file}",
    *list,
    pdfmarks)
  fail "Problem in merge_pdfs" unless status.success?
end

.page_count(pdf_file) ⇒ Fixnum

Extract pdf page count using pdf-reader

Returns:

  • (Fixnum)

    the page count of the given pdf file



45
46
47
48
49
50
51
52
# File 'lib/pdfs2pdf/pdfs2pdf.rb', line 45

def page_count(pdf_file)
  pdf_file = File.expand_path(pdf_file)
  File.open(pdf_file, "rb") do |io|
    reader = PDF::Reader.new(io)
    return reader.page_count
  end
  fail "Problem getting the page count for #{pdf_file}"
end

.update_configObject

Customize the configuration for specific system (Ubuntu/OSX/etc) See: ./lib/pdfs2pdf/configuration.rb for available options



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pdfs2pdf/config/pdfs2pdf.rb', line 5

def update_config
  Pdfs2Pdf.configure do |_config|
    # Note: add your custom config here
    # config.gs_binary = '/usr/bin/gs'
    #
    # config.default_options = {
    #   paper_size: 'Letter',
    #   ..
    # }
    # config.pdfmarks_meta   = ..
  end
end