Module: VivlioStarter::Pdf::Utilities

Defined in:
lib/vivlio_starter/cli/pdf/utilities.rb

Overview

HexaPDF を使った PDF 共通ユーティリティ

Class Method Summary collapse

Class Method Details

.ensure_blank_page_pdf(path, width_pt, height_pt) ⇒ Object

空白ページ PDF を生成する

Parameters:

  • path (String)
  • width_pt (Float)
  • height_pt (Float)


33
34
35
36
37
38
39
40
# File 'lib/vivlio_starter/cli/pdf/utilities.rb', line 33

def ensure_blank_page_pdf(path, width_pt, height_pt)
  return path if File.exist?(path)

  doc = HexaPDF::Document.new
  doc.pages.add([0, 0, width_pt, height_pt])
  doc.write(path, optimize: true)
  path
end

.page_count(file) ⇒ Integer?

PDF のページ数を取得する

Parameters:

  • file (String)

Returns:

  • (Integer, nil)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vivlio_starter/cli/pdf/utilities.rb', line 14

def page_count(file)
  return nil unless File.exist?(file)

  if system("which pdfinfo >/dev/null 2>&1")
    info = `pdfinfo "#{file}" 2>/dev/null`
    pages = info[/^Pages:\s+(\d+)/i, 1]
    return pages.to_i if pages
  end

  doc = HexaPDF::Document.open(file)
  doc.pages.count
rescue StandardError
  nil
end