From c13f822f47622bb9efc8d7f5630ae2f6773cdb04 Mon Sep 17 00:00:00 2001 From: Brendan Weibrecht Date: Sun, 17 May 2015 21:16:10 +1000 Subject: [PATCH] Allow custom margins (differing values per side) --- lib/shrimp/phantom.rb | 2 +- lib/shrimp/rasterize.js | 2 +- spec/shrimp/phantom_spec.rb | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/shrimp/phantom.rb b/lib/shrimp/phantom.rb index 9b689ff..10fd650 100644 --- a/lib/shrimp/phantom.rb +++ b/lib/shrimp/phantom.rb @@ -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] diff --git a/lib/shrimp/rasterize.js b/lib/shrimp/rasterize.js index 731382e..e74d16b 100644 --- a/lib/shrimp/rasterize.js +++ b/lib/shrimp/rasterize.js @@ -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]; diff --git a/spec/shrimp/phantom_spec.rb b/spec/shrimp/phantom_spec.rb index 9d4978b..c44e34e 100644 --- a/spec/shrimp/phantom_spec.rb +++ b/spec/shrimp/phantom_spec.rb @@ -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" @@ -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"