tcocca / active_pdftk

ruby wrapper for the pdftk command line utility for working with editable pdf files
MIT License
46 stars 37 forks source link

Accept StringIO in the :pdf option #32

Closed restebanez closed 12 years ago

restebanez commented 12 years ago

Hi,

I'm not sure if i understand this comment in /lib/active_pdftk/wrapper.rb correctly:

# @option ranges [String, File, StringIO, Tempfile] :pdf optional unless multiple inputs are required the file to run the range on.

Does it mean i can pass any of those data types?

I'd like to pass a StringIO instead of a pdf path, but unfortunately it doesn't work:

@pdftk.cat([{:stringio => StringIO.new(pdf_raw_content)},{:pdf=> pdf_file_path}])
ActivePdftk::CommandError: sh: StringIO:0x007faa004abc10: No such file or directory
#!# While executing #=> `pdftk B=/Users/restebanez/ctms-sandbox-PIR.pdf C=#<StringIO:0x007faa004abc10> cat B C output -`

Thanks,

Rodrigo

tcocca commented 12 years ago

Hi,

That comment means that any of the types [String, File, StringIO, Tempfile] are acceptable for the :pdf option so you can try:

@pdftk.cat([{:pdf => StringIO.new(pdf_raw_content)}, {:pdf=> pdf_file_path}])

Also, the types should be able to be mixed as well, 1 as a StringIO, 1 as a path, it should all work correctly.

Let me know if that works. ~ Tom

PS. I need to finish up a few last things on this gem before I release it. Hoping to find the time to get to that soon.

restebanez commented 12 years ago

hi @tcocca

Thanks for your fast answer. Unfortunately I can't make it work in my machine, i think this piece of code may proof a bug:

@pdftk = ActivePdftk::Wrapper.new
pir="/Users/restebanez/ctms-sandbox-PIR.pdf"
string_file=File.read(pir)
raw=@pdftk.cat([{:pdf => StringIO.new(string_file)},{:pdf=> pir}])
ActivePdftk::CommandError: sh: StringIO:0x007faa006c4f60: No such file or directory
#!# While executing #=> `pdftk B=#<StringIO:0x007faa006c4f60> C=/Users/restebanez/ctms-sandbox-PIR.pdf cat B C output -`

It looks like active_pdftk thinks that #StringIO:0x007faa006c4f60 is a file name. I'm using Ruby 1.9.2p290 in a OS X 10.7

Thanks,

Rodrigo

tcocca commented 12 years ago

Ah, that documentation line might be a typo actually.

Looking through my specs here: https://github.com/tcocca/active_pdftk/blob/master/spec/active_pdftk/wrapper_spec.rb#L218-230

cat and #shuffle are the only 2 pdftk calls where I am not looping the input type, and forcing it to be file paths. This might be the issue, that cat and shuffle can only handle paths and not input. If you could verify this with the command direction through the CLI I would appreciate it. Otherwise I will try and give this a shot later tonight. For now you can just use the path to the file.

However in you code above, you were trying to cat a file that you read in to StringIO onto the same file that you just read. So that code above (if it was working) would just return StringIO of the file twice ...

I'm not sure what exactly you are trying to do here.

Anyway take a look through the docs and figure out if its even possibly to send input to #cat or #shuffle (i'm not sure it is possible to give those commands stdin like that).

~ Tom

restebanez commented 12 years ago

Thinking about it, there isn't any command that can accept more than one file as STDIN option because there is only one STDIN. A workaround would be to create a tmp file whenever it receives a StringIO.

I should have said that my code was only for showing the bug, yeah the output would generate the same PDF twice.

Thanks for your help

Rodrigo