Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Allow custom margins (differing values per side) #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/shrimp/phantom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def run!
# Public: Returns the phantom rasterize command
def cmd
cookie_file = dump_cookies
format, zoom, margin, orientation = options[:format], options[:zoom], options[:margin], options[:orientation]
format, zoom, margin, orientation = options[:format], options[:zoom], "'#{options[:margin].to_json}'", options[:orientation]
rendering_time, timeout = options[:rendering_time], options[:rendering_timeout]
viewport_width, viewport_height = options[:viewport_width], options[:viewport_height]
max_redirect_count = options[:max_redirect_count]
Expand Down
2 changes: 1 addition & 1 deletion lib/shrimp/rasterize.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ if (system.args.length < 3 || system.args.length > 13) {
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
size = system.args[3].split('*');
page_options.paperSize = size.length === 2 ? { width:size[0], height:size[1], margin:'0px' }
: { format:system.args[3], orientation:orientation, margin:margin };
: { format:system.args[3], orientation:orientation, margin: JSON.parse(margin) };
}
if (system.args.length > 4) {
page_options.zoomFactor = system.args[4];
Expand Down
13 changes: 12 additions & 1 deletion spec/shrimp/phantom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ def valid_pdf(io)
phantom.outfile.should eq "#{Dir.tmpdir}/test.pdf"
end

it "should accept margin provided as a hash of sides" do
margin = {
top: '4.6mm',
right: '4mm',
left: '3cm',
# bottom: '0cm', # default
}
phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => margin }, { }, "#{Dir.tmpdir}/test.pdf")
phantom.options[:margin].should eq margin
end

it "should render a pdf file" do
#phantom = Shrimp::Phantom.new("file://#{@path}")
#phantom.to_pdf("#{Dir.tmpdir}/test.pdf").first should eq "#{Dir.tmpdir}/test.pdf"
Expand All @@ -59,7 +70,7 @@ def valid_pdf(io)

it "should parse options into a cmd line" do
phantom = Shrimp::Phantom.new("file://#{testfile}", { :margin => "2cm", :max_redirect_count => 10 }, { }, "#{Dir.tmpdir}/test.pdf")
phantom.cmd.should include "test.pdf A4 1 2cm portrait"
phantom.cmd.should include "test.pdf A4 1 '\"2cm\"' portrait"
phantom.cmd.should include "file://#{testfile}"
phantom.cmd.should include "lib/shrimp/rasterize.js"
phantom.cmd.should end_with " 10"
Expand Down