Upload PDF / Save as tiff

Hi,

I'm trying to automate the conversion of a PDF document received via a browser upload to a tiff image via ghostscript. I have the PDF data in a string, and I need the tiff data returned into a string.

The general command I want to emulate is: type test.pdf | "c:\program files\gs\gs8.56\bin\gswin32c.exe" -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -sOutputFile=- > test5.tiff

I've had success in writing directly to a file via: IO.popen('c:\program files\gs\gs8.56\bin\gswin32c.exe -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -sOutputFile=test3.tiff -', "wb") do |pipe|    pipe.write pdf_data end

However, I really want to bypass writing to disk. If I attempt to get the data back from the pipe, there are extra bytes returned at the end of the stream. On my test file, I receive eight extra bytes on XP, and ten extra bytes on Linux.

# **** This doesn't work **** # test conversion of the pdf to tiff via standard input, back to standard output # There are eight extra bytes received on the test file. IO.popen('c:\program files\gs\gs8.56\bin\gswin32c.exe -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -sOutputFile=- -', "wb+") do |pipe|    pipe.write pdf_data    pipe.close_write    tiff_data = pipe.read    open('test1.tiff', 'wb') { |f| f.syswrite tiff_data } end

Is it not possible to receive binary data correctly from the pipe? I've tested on both XP (ruby 1.8.4) and Linux (ruby 1.8.4).

Thanks for any advice you can offer.

Regards,

Rich Duzenbury

From: rubyonrails-talk@googlegroups.com [mailto:rubyonrails- talk@googlegroups.com] On Behalf Of Stephan Wehner Sent: Wednesday, April 25, 2007 1:12 PM To: rubyonrails-talk@googlegroups.com Subject: [Rails] Re: Upload PDF / Save as tiff

And then again, look at this

# cat /tmp/t.pdf | /opt/local/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -sOutputFile=- - | wc       14 263 11458 # cat /tmp/t.pdf | /opt/local/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -sOutputFile=- - > out.tiff # ls -l out.tiff -rw-r--r-- 1 username username 11450 Apr 25 11:07 out.tiff

In other words, running your command in a bash shell produces a

similar

problem - 8 bytes difference.

11458 is the length of the file produced by the ruby program as well.

Thanks!

That's a bit of lovely news. I'll see if I can't figure out why piping v redirecting doesn't' work on gs.

Regards, Rich

From: rubyonrails-talk@googlegroups.com [mailto:rubyonrails- talk@googlegroups.com] On Behalf Of Stephan Wehner Sent: Thursday, April 26, 2007 1:26 PM To: rubyonrails-talk@googlegroups.com Subject: [Rails] Re: Upload PDF / Save as tiff

> > Thanks! > > That's a bit of lovely news. I'll see if I can't figure out why

piping v

> redirecting doesn't' work on gs. > > Regards, > Rich Actually I'm finding the problem depends on the device.

Here is the ruby script

Hi,

I found out what is going on. Two kind souls on comp.lang.postscript gave the answer:

"Yes, that's right. gs' tiff devices generate TIFF files which contain the TIFF header at the beginning. As some of the tag values are only known at the end of the conversion, these values are updated then, which requires a seekable output file."

I suspect the other file formats you examined have the same issue.

Regards, Rich Duzenbury