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

NOMBRE_FONT_RELATIVE =

隠しノンブルに使う埋め込み可能フォント(書籍プロジェクト同梱の TTF)。HexaPDF 組込の “Helvetica” は標準 14 フォントで PDF に埋め込まれず、印刷所入稿で「非埋め込みフォント」事故になる(FT-02)。プロジェクト直下に同梱のHackGen35ConsoleNF があれば登録してサブセット埋め込みする(数字のみで極小)。

File.join("stylesheets", "fonts", "hackgen35", "HackGen35ConsoleNF-Regular.ttf")
NOMBRE_FONT_NAME =

document.config の font.map へ登録する内部フォント名

"VivlioNombre"
FALLBACK_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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 71

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 が存在しなければ生成する



33
34
35
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 33

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 のページ数を取得する



28
29
30
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 28

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vivlio_starter/cli/pdf/enhanced_provider.rb', line 42

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]…")

  font = register_nombre_font(document)

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

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