Class: VivlioStarter::Pdf::EnhancedProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_starter/cli/pdf/enhanced_provider.rb

Overview

vivlio-starter 本体から呼び出される HexaPDF ベースのプロバイダ

隠しノンブル書き込み・PDF アウトライン付与など、Enhanced Mode 固有の PDF 操作を提供する。

Constant Summary collapse

FONT_NAME =

隠しノンブルに使用するフォント名

"Helvetica"
FONT_SIZE_PT =

隠しノンブルのフォントサイズ(pt)

6

Instance Method Summary collapse

Instance Method Details

#add_outline!(pdf_path, items, max_level:) ⇒ Boolean

PDF にアウトライン(しおり)を付与するOutlineWriter を使い、階層構造を HexaPDF のアウトラインツリーに変換する

Parameters:

  • pdf_path (String)

    対象 PDF のパス

  • items (Array<Hash>)

    アウトライン項目(:level, :text, :page)

  • max_level (Integer)

    アウトラインの最大階層深度

Returns:

  • (Boolean)

    成功なら true



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 62

def add_outline!(pdf_path, items, max_level:)
  return false unless File.exist?(pdf_path)

  document = HexaPDF::Document.open(pdf_path)
  writer = OutlineWriter.new(document, max_level:, on_skip: method(:log_outline_skip))
  inserted = writer.write(items)
  if inserted.zero?
    LogHelper.log_warn("[OutlineWriter] 有効なアウトライン項目が存在しないためスキップしました")
    return false
  end

  document.write(pdf_path, optimize: true)
  LogHelper.log_success("[OutlineWriter] PDF にアウトラインを #{inserted} 件追加しました")
  true
rescue StandardError => e
  LogHelper.log_error("[OutlineWriter] PDF アウトライン付与に失敗: #{e.message}")
  false
end

#ensure_blank_page_pdf(path, width_pt, height_pt) ⇒ Object

空白ページ PDF が存在しなければ生成する



26
27
28
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 26

def ensure_blank_page_pdf(path, width_pt, height_pt)
  Utilities.ensure_blank_page_pdf(path, width_pt, height_pt)
end

#page_count(pdf_path) ⇒ Object

PDF のページ数を取得する



21
22
23
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 21

def page_count(pdf_path)
  Utilities.page_count(pdf_path)
end

#stamp_nombre!(pdf_path, bleed_pt:) ⇒ Boolean

PDF の各ページに隠しノンブル(ページ番号)を書き込む奇数ページは左端、偶数ページは右端に 90° 回転して配置する

Parameters:

  • pdf_path (String)

    対象 PDF のパス

  • bleed_pt (Float)

    塗り足し幅(pt)

Returns:

  • (Boolean)

    成功なら true



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 35

def stamp_nombre!(pdf_path, bleed_pt:)
  return false unless File.exist?(pdf_path)

  document = HexaPDF::Document.open(pdf_path)
  total = document.pages.count
  return false if total.zero?

  LogHelper.log_action("[NombreStamper] 隠しノンブルを書き込みます(#{total} ページ)[Enhanced Mode]…")

  document.pages.each_with_index do |page, idx|
    stamp_page(page, idx + 1, bleed_pt: bleed_pt.to_f)
  end

  document.write(pdf_path, optimize: true)
  LogHelper.log_success("[NombreStamper] 隠しノンブル書き込み完了(#{total} ページ)")
  true
rescue StandardError => e
  LogHelper.log_error("[NombreStamper] 隠しノンブル書き込みに失敗: #{e.message}")
  false
end