Module: StackedPdfGenerator::CropMarksProcessor

Defined in:
lib/stacked_pdf_generator/crop_marks_processor.rb

Overview

Applies a uniform crop rectangle (in PDF points) to every page of a source PDF, writing a new PDF whose CropBox and MediaBox match the requested region. Used as a pre-processing step before the imposition pipeline when the input contains printer crop marks.

Class Method Summary collapse

Class Method Details

.call(input_path, output_path, box) ⇒ Object

Crop input_pathoutput_path using the given box (a 4-element array [left, bottom, right, top] in PDF points).



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stacked_pdf_generator/crop_marks_processor.rb', line 15

def call(input_path, output_path, box)
  validate_box!(box)
  doc = HexaPDF::Document.open(input_path)
  doc.pages.each do |page|
    page[:CropBox] = box.dup
    page[:MediaBox] = box.dup
    page.delete(:BleedBox)
    page.delete(:TrimBox)
    page.delete(:ArtBox)
  end
  doc.write(output_path, optimize: true)
  output_path
end

.validate_box!(box) ⇒ Object

Raises:



29
30
31
32
33
34
35
# File 'lib/stacked_pdf_generator/crop_marks_processor.rb', line 29

def validate_box!(box)
  raise ProcessingError, 'Crop box must have 4 numeric values' unless box.is_a?(Array) && box.length == 4

  left, bottom, right, top = box.map(&:to_f)
  raise ProcessingError, "Crop box has zero or negative width (left=#{left}, right=#{right})" if right <= left
  raise ProcessingError, "Crop box has zero or negative height (bottom=#{bottom}, top=#{top})" if top <= bottom
end