Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added auto-closing #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/autoprint_closing.rb
@@ -0,0 +1,13 @@
require "rubygems"
$:.push File.join(File.dirname(__FILE__), "../lib")
require "prawn-print"

FILENAME = "/tmp/prawn_autoprint_named.pdf"

pdf = Prawn::Document.new
pdf.text "Help! I am trapped in a PDF factory!"
pdf.autoprint "LaserJet", true
pdf.render_file FILENAME

`open -a "Adobe Reader" #{FILENAME}` if system("which open")
puts "Done."
18 changes: 13 additions & 5 deletions lib/prawn-print.rb
Expand Up @@ -7,23 +7,23 @@ module Print
include JS

def print
print_with_auto_to_printer(false, nil)
print_with_auto_to_printer(false, nil, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, weird default for a boolean.

end

def autoprint(printer=nil)
print_with_auto_to_printer(true, printer)
def autoprint(printer=nil, close=nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil is a weird default for a boolean. false?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right... Still practising ruby...

print_with_auto_to_printer(true, close, printer)
end

private

def print_with_auto_to_printer(auto, printer)
def print_with_auto_to_printer(auto, close, printer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect a method named print_with_auto_to_printer to take arguments in the order auto, printer. Maybe rename to print_with_auto_to_printer_and_close or something, and have arguments in that order. Or pass in named keywords.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closeDoc() closes the PDF document. It's a pity it's not possible to minimize the window or close Adobe Reader completely. Adobe removed this functionality for "security reasons"...

add_docopen_js("print", <<-JS)

var pp = this.getPrintParams();
#{interactive_js(auto)}
#{select_printer_js(printer)}
this.print(pp);

#{close_js(close)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simplify this by just inlining it. Probably not worth having an extra method. So just #{"this.closeDoc();" if close}

JS
end

Expand Down Expand Up @@ -55,6 +55,14 @@ def select_printer_js(printer)
end
end

def close_js(close)
if close
<<-JS
this.closeDoc();
JS
end
end

end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/prawn-print/version.rb
@@ -1,5 +1,5 @@
module Prawn
module Print
VERSION = "0.0.3"
VERSION = "0.0.4"
end
end
11 changes: 8 additions & 3 deletions spec/prawn-print_spec.rb
Expand Up @@ -7,18 +7,23 @@
describe Prawn::Print, "#print" do
it "should work without arguments" do
pdf = Prawn::Document.new
lambda { pdf.print }.should_not raise_error
expect(lambda { pdf.print }).not_to raise_error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just expect { pdf.print }.not_to is actually fine.

end
end

describe Prawn::Print, "#autoprint" do
it "should work without arguments" do
pdf = Prawn::Document.new
lambda { pdf.autoprint }.should_not raise_error
expect(lambda { pdf.autoprint }).not_to raise_error
end

it "should work with an argument" do
pdf = Prawn::Document.new
lambda { pdf.autoprint("LaserJet") }.should_not raise_error
expect(lambda { pdf.autoprint("LaserJet") }).not_to raise_error
end

it "should work with two arguments" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were admittedly awful before, but "with two arguments" is quite unclear. Maybe it "should accept a printer name and a 'close' bool" or something?

pdf = Prawn::Document.new
expect(lambda { pdf.autoprint("LaserJet", true) }).not_to raise_error
end
end