Module: Sevgi::Sundries::Export
- Extended by:
- Export
- Included in:
- Export
- Defined in:
- lib/sevgi/sundries/export.rb,
lib/sevgi/sundries/export/native.rb,
lib/sevgi/sundries/export/system.rb
Overview
Exports SVG content and post-processes PDF output.
Native PDF/PNG rendering is loaded lazily so installing sevgi-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.
Defined Under Namespace
Modules: Renderer
Constant Summary collapse
- AVAILABLE =
Supported export format names mapped to file extensions.
(EXTENSIONS = { ".pdf" => :pdf, ".png" => :png }.freeze) .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) ⇒ Sevgi::Function::Shell::Result
Places a single A5 PDF page twice on an A4 landscape sheet 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) {|svg| ... } ⇒ Object
Exports SVG source to a PDF or PNG file using librsvg and Cairo.
-
#format_for!(format, output) ⇒ Symbol
Resolves the export format from an explicit value or output extension.
-
#inject(svg, css) ⇒ String
Inserts CSS before the closing svg tag.
-
#inkscape(infile, outfile = nil, format: nil, background: "#ffffff", opacity: 1, width: nil, height: nil, id: nil, page: nil, css: nil) ⇒ Sevgi::Function::Shell::Result
Exports an SVG file through Inkscape.
-
#rsvg(infile, outfile = nil, format: nil, background: "#ffffff", width: nil, height: nil, id: nil, css: nil) ⇒ Sevgi::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) ⇒ Sevgi::Function::Shell::Result
Merges PDF files with pdfunite.
Instance Method Details
#a5_on_a4(infile, outfile) ⇒ Sevgi::Function::Shell::Result
Places a single A5 PDF page twice on an A4 landscape sheet with pdfcpu.
16 |
# File 'lib/sevgi/sundries/export/system.rb', line 16 def a5_on_a4(infile, outfile) = F.sh!("pdfcpu", "nup", "--", "form:A4L, border:off", outfile, "2", infile) |
#a5_on_a4!(infile) ⇒ void
This method returns an undefined value.
Replaces a PDF file with an A5-on-A4 layout generated by pdfcpu.
23 24 25 26 27 28 29 |
# File 'lib/sevgi/sundries/export/system.rb', line 23 def a5_on_a4!(infile) temp = Tempfile.new(%w[output .pdf], File.dirname(infile)) a5_on_a4(infile, temp.path) FileUtils.mv(temp.path, infile) ensure temp&.close! end |
#call(svg, output, format: nil, width: nil, height: nil, dpi: DEFAULT_DPI, css: nil) {|svg| ... } ⇒ Object
Exports SVG source to a PDF or PNG file using librsvg and Cairo.
54 |
# File 'lib/sevgi/sundries/export.rb', line 54 def call(*args, **kwargs, &block) = native!.call(*args, **kwargs, &block) |
#format_for!(format, output) ⇒ Symbol
Resolves the export format from an explicit value or output extension.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sevgi/sundries/export.rb', line 61 def format_for!(format, output) if format format = format.to_sym ExportError.("Unsupported export format: #{format}") unless AVAILABLE.key?(format) format else ext = File.extname(output.to_s).downcase ExportError.("Unrecognized file extension: #{ext}") unless EXTENSIONS.key?(ext) EXTENSIONS[ext] end end |
#inject(svg, css) ⇒ String
Inserts CSS before the closing svg tag.
80 |
# File 'lib/sevgi/sundries/export.rb', line 80 def inject(svg, css) = svg.sub("</svg>", "<style>#{css}</style></svg>") |
#inkscape(infile, outfile = nil, format: nil, background: "#ffffff", opacity: 1, width: nil, height: nil, id: nil, page: nil, css: nil) ⇒ Sevgi::Function::Shell::Result
Exports an SVG file through Inkscape.
47 48 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 83 84 85 86 87 88 89 |
# File 'lib/sevgi/sundries/export/system.rb', line 47 def inkscape( infile, outfile = nil, format: nil, background: "#ffffff", opacity: 1, width: nil, height: nil, id: nil, page: nil, css: nil ) infile = File.(infile) outfile ||= F.subext(".pdf", infile) outfile = File.(outfile) format = format_for!(format, outfile) if css temp = Tempfile.new(%w[input .svg], File.dirname(infile)) ::File.write(temp.path, inject(::File.read(infile), css)) infile = temp.path end 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}", infile ].compact ) ensure temp&.close! end |
#rsvg(infile, outfile = nil, format: nil, background: "#ffffff", width: nil, height: nil, id: nil, css: nil) ⇒ Sevgi::Function::Shell::Result
Exports an SVG file through rsvg-convert.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sevgi/sundries/export/system.rb', line 105 def rsvg( infile, outfile = nil, format: nil, background: "#ffffff", width: nil, height: nil, id: nil, css: nil ) infile = File.(infile) outfile ||= F.subext(".pdf", infile) outfile = File.(outfile) format = format_for!(format, outfile) if css temp = Tempfile.new(%w[input .svg], File.dirname(infile)) ::File.write(temp.path, inject(::File.read(infile), css)) infile = temp.path end 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}", infile ].compact ) ensure temp&.close! end |
#stamp(infile, outfile, stamp:, placeholder:) ⇒ Boolean
Replaces exact placeholder text objects in PDF streams. The placeholder must appear as a PDF literal string in a white text object matching the export stamp pattern. Replacement text is escaped as a PDF literal string. When no exact match is replaced, the output file is not written.
90 |
# File 'lib/sevgi/sundries/export.rb', line 90 def stamp(infile, outfile, stamp:, placeholder:) = native!.stamp(infile, outfile, stamp:, placeholder:) |
#stamp!(infile, stamp:, placeholder:) ⇒ Boolean
Replaces exact placeholder text objects inside a PDF file in place. The input file is replaced only after at least one exact placeholder match is rewritten into a non-empty output file.
99 |
# File 'lib/sevgi/sundries/export.rb', line 99 def stamp!(infile, stamp:, placeholder:) = native!.stamp!(infile, stamp:, placeholder:) |
#unite(sources, outfile) ⇒ Sevgi::Function::Shell::Result
Merges PDF files with pdfunite.
149 |
# File 'lib/sevgi/sundries/export/system.rb', line 149 def unite(sources, outfile) = F.sh!("pdfunite", *sources, outfile) |