Module: Sumi::Sundries::Export
- Extended by:
- Export
- Included in:
- Export
- Defined in:
- sundries/lib/sumi/sundries/export.rb,
sundries/lib/sumi/sundries/export/input.rb,
sundries/lib/sumi/sundries/export/system.rb,
sundries/lib/sumi/sundries/export/staging.rb,
sundries/lib/sumi/sundries/export/metadata.rb,
sundries/lib/sumi/sundries/export/native/stamp.rb,
sundries/lib/sumi/sundries/export/native/render.rb,
sundries/lib/sumi/sundries/export/metadata/fields.rb,
sundries/lib/sumi/sundries/export/metadata/packet.rb,
sundries/lib/sumi/sundries/export/native/stamp/scanner.rb
Overview
Exports SVG source to PNG or PDF and post-processes PDF output.
Native PDF/PNG rendering is loaded lazily so installing sumi-sundries for SVG-only helpers does not require the
Cairo, RSVG, or HexaPDF gems. Native export entrypoints raise MissingComponentError when those optional
gems are unavailable. Omit format: to infer it from the output suffix.
width and height are output dimensions rather than changes to the SVG
viewBox. The return value is the expanded path that was written.
Export-only CSS is a last-minute adjustment, not a replacement for document styles or validation. Insertion
requires well-formed SVG ending in an unprefixed </svg> followed only by XML whitespace. Self-closing roots,
prefixed roots, and trailing comments are unsupported with CSS. CSS is XML-escaped but not parsed or validated.
The source callback runs after insertion and before ordinary or prepared conversion.
Constant Summary collapse
- AVAILABLE =
Supported export format names mapped to file extensions.
EXTENSIONS.invert.freeze
- DEFAULT_DPI =
Default SVG CSS pixel density.
96.0- ExportError =
Raised when SVG export or PDF post-processing cannot be completed.
Class.new(Error)
Instance Method Summary collapse
-
#a5_on_a4(infile, outfile) ⇒ Sumi::Function::Shell::Result
Places A5 PDF pages on A4 landscape sheets with pdfcpu.
-
#a5_on_a4!(infile) ⇒ void
Replaces a PDF file with an A5-on-A4 layout generated by pdfcpu.
-
#call(svg, output, format: nil, width: nil, height: nil, dpi: DEFAULT_DPI, css: nil, declaration: nil, preview: nil, fallback: true) {|svg| ... } ⇒ String
Exports SVG source through ordinary rendering or optional Toji preparation.
-
#inkscape(infile, outfile = nil, format: nil, background: "#ffffff", opacity: 1, width: nil, height: nil, id: nil, page: nil, css: nil) ⇒ Sumi::Function::Shell::Result
Exports an SVG file through Inkscape.
-
#metadata(infile, outfile, **fields) ⇒ String
Writes supplied publication metadata to PDF XMP and matching legacy Info entries.
-
#metadata!(infile, **fields) ⇒ String
Writes publication metadata atomically to an existing PDF.
-
#rsvg(infile, outfile = nil, format: nil, background: "#ffffff", width: nil, height: nil, id: nil, css: nil) ⇒ Sumi::Function::Shell::Result
Exports an SVG file through rsvg-convert.
-
#stamp(infile, outfile, stamp:, placeholder:) ⇒ Boolean
Replaces exact placeholder text objects in PDF streams.
-
#stamp!(infile, stamp:, placeholder:) ⇒ Boolean
Replaces exact placeholder text objects inside a PDF file in place.
-
#unite(sources, outfile) ⇒ Sumi::Function::Shell::Result
Merges PDF files with pdfunite.
Instance Method Details
#a5_on_a4(infile, outfile) ⇒ Sumi::Function::Shell::Result
Places A5 PDF pages on A4 landscape sheets with pdfcpu.
15 16 17 |
# File 'sundries/lib/sumi/sundries/export/system.rb', line 15 def a5_on_a4(infile, outfile) F.sh!("pdfcpu", "nup", "--conf", "disable", "--", "form:A4L, border:off", outfile, "2", infile) end |
#a5_on_a4!(infile) ⇒ void
This method returns an undefined value.
Replaces a PDF file with an A5-on-A4 layout generated by pdfcpu.
24 25 26 27 28 29 30 31 |
# File 'sundries/lib/sumi/sundries/export/system.rb', line 24 def a5_on_a4!(infile) Staging.replace(infile, prefix: ".sumi-nup-", filename: "output.pdf") do |outfile| a5_on_a4(infile, outfile) true end nil end |
#call(svg, output, format: nil, width: nil, height: nil, dpi: DEFAULT_DPI, css: nil, declaration: nil, preview: nil, fallback: true) {|svg| ... } ⇒ String
Exports SVG source through ordinary rendering or optional Toji preparation. An Imprint declaration or preview selects Toji automatically for PDF output. A preview alone uses an empty target and parameter declaration. Other documents use ordinary export. Fallback warns and uses ordinary PDF export after a preparation failure. CSS and the source callback run once before either backend receives the source.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'sundries/lib/sumi/sundries/export.rb', line 86 def call( svg, output, format: nil, width: nil, height: nil, dpi: DEFAULT_DPI, css: nil, declaration: nil, preview: nil, fallback: true, &block ) output = output_path(output) format = format_for(format, output) input = Input.new(svg, {format:, width:, height:, dpi:, css:, declaration:, preview:, fallback:}, &block) if format == :pdf && !input..empty? Metadata.export(output, input.) { |path| produce(input, path, format:) } else produce(input, output, format:) end end |
#inkscape(infile, outfile = nil, format: nil, background: "#ffffff", opacity: 1, width: nil, height: nil, id: nil, page: nil, css: nil) ⇒ Sumi::Function::Shell::Result
Exports an SVG file through Inkscape.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'sundries/lib/sumi/sundries/export/system.rb', line 49 def inkscape( infile, outfile = nil, format: nil, background: "#ffffff", opacity: 1, width: nil, height: nil, id: nil, page: nil, css: nil ) infile, outfile, format = prepare_paths(infile, outfile, format) with_staged_svg(infile, css) do |source| F.sh!( *[ "inkscape", "--batch-process", "--actions=select-by-class:text,object-to-path", "--export-type=#{format}", ("--export-background=#{background}" if background), ("--export-background-opacity=#{opacity}" if opacity), ("--export-width=#{width}" if width), ("--export-height=#{height}" if height), ("--export-id=#{id}" if id), ("--export-id-only" if id), ("--export-page=#{page}" if page), "--export-filename=#{outfile}", source ].compact ) end end |
#metadata(infile, outfile, **fields) ⇒ String
Writes supplied publication metadata to PDF XMP and matching legacy Info entries. The destination is replaced only after the PDF and metadata have been written.
158 159 160 161 162 163 |
# File 'sundries/lib/sumi/sundries/export.rb', line 158 def (infile, outfile, **fields) fields = Metadata.capture(fields) source = F.path(infile, context: "PDF metadata input") output = output_path(outfile) Metadata.export(output, fields) { |path| FileUtils.cp(source, path) } end |
#metadata!(infile, **fields) ⇒ String
Writes publication metadata atomically to an existing PDF.
174 |
# File 'sundries/lib/sumi/sundries/export.rb', line 174 def (infile, **fields) = (infile, infile, **fields) |
#rsvg(infile, outfile = nil, format: nil, background: "#ffffff", width: nil, height: nil, id: nil, css: nil) ⇒ Sumi::Function::Shell::Result
Exports an SVG file through rsvg-convert.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'sundries/lib/sumi/sundries/export/system.rb', line 98 def rsvg( infile, outfile = nil, format: nil, background: "#ffffff", width: nil, height: nil, id: nil, css: nil ) infile, outfile, format = prepare_paths(infile, outfile, format) with_staged_svg(infile, css) do |source| F.sh!( *[ "rsvg-convert", "--format=#{format}", ("--background-color=#{background}" if background), ("--width=#{width}" if width), ("--height=#{height}" if height), ("--export-id=#{id}" if id), "--output=#{outfile}", source ].compact ) end end |
#stamp(infile, outfile, stamp:, placeholder:) ⇒ Boolean
Streams with unbalanced graphics-state or text-object operators are left unchanged.
Replaces exact placeholder text objects in PDF streams. The placeholder must be a PDF literal string inside a white text object that matches Sumi's stamp pattern. Replacement text is escaped as a PDF literal string. When no match exists, the method writes no output file.
121 122 123 124 |
# File 'sundries/lib/sumi/sundries/export.rb', line 121 def stamp(infile, outfile, stamp:, placeholder:) native! Stamp.stamp(infile, outfile, stamp:, placeholder:) end |
#stamp!(infile, stamp:, placeholder:) ⇒ Boolean
Streams with unbalanced graphics-state or text-object operators are left unchanged.
Replaces exact placeholder text objects inside a PDF file in place. The input file changes only after at least one exact match produces a nonempty output file.
135 136 137 138 |
# File 'sundries/lib/sumi/sundries/export.rb', line 135 def stamp!(infile, stamp:, placeholder:) native! Stamp.stamp!(infile, stamp:, placeholder:) end |
#unite(sources, outfile) ⇒ Sumi::Function::Shell::Result
Merges PDF files with pdfunite.
133 |
# File 'sundries/lib/sumi/sundries/export/system.rb', line 133 def unite(sources, outfile) = F.sh!("pdfunite", *sources, outfile) |