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

Null Byte #9

Closed
jordanmichaelrushing opened this issue Dec 31, 2014 · 3 comments
Closed

Null Byte #9

jordanmichaelrushing opened this issue Dec 31, 2014 · 3 comments
Labels

Comments

@jordanmichaelrushing
Copy link

So I'm pulling down a remote PDF file on Heroku. I want to add a watermark on it. This works perfect locally on Mac, but in production on Heroku it's failing. This is how I'm doing it

So I'm pulling down a remote PDF file on Heroku. I want to add a watermark on it. This works perfect locally on Mac, but in production on Heroku it's failing. This is how I'm doing it

def pdf(number)
require 'open-uri'
company_logo = CombinePDF.new("logo.pdf").pages[0]
open("#{number}.pdf", 'wb') do |file|
file << open("http://patentimages.storage.googleapis.com/pdfs/#{number}.pdf").read
end

pdf = CombinePDF.new open("#{number}.pdf").read
pdf.pages.each {|page| page << company_logo}
pdf.save "#{number}.pdf"
end
However this line is failing:
pdf = CombinePDF.new contents
With this:
ArgumentError: string contains null byte

Also before the error I see this:
PDF 1.5 Object streams found - they are not fully supported! attempting to extract objects.
Again this works perfectly locally on Mac without any changes in code. What am I missing?

Btw, number in this case is "US5409533"

@boazsegev
Copy link
Owner

Thanks for opening the issue.

Because you mentioned the code is working fine on your mac (but not on Heroku), I'd like to try a few things before I start looking into the code on my end.

I would like to start with changing the file access code, removing the temporary files and using the CombinePDF.parse to read the raw data (instead of CombinePDF.new that accepts filenames).

I suggest this change because normally Heroku acts up when using file writing permissions (slugs are read-only, that's also why sqlite cannot be used on Heroku).

Also, you were passing the CombinePDF.new method the PDF's content (instead of a file name).

The last reason for which I recommend avoiding tmp files (aside of performance), is the issue of simultaneous access... It's just safer to avoid a system resource that might be in use by a parallel request.

Have a look at this, try it out and let me know how it works:

require 'open-uri'

def pdf(number)

# loading the logo seems fine, as we are only reading from a file
company_logo = CombinePDF.new("logo.pdf").pages[0]

# I changed the CombinePDF.new(filename) to CombinePDF.parse(pdf_stream),
# so there's no need to save a tmp file.
pdf = CombinePDF.parse open("http://patentimages.storage.googleapis.com/pdfs/#{number}.pdf").read

# stamping each page would work if the parsing actually worked
pdf.pages.each {|page| page << company_logo}

# saving in Heroku WILL **PROBABLY** FAIL(!), so this is just for testing:
pdf.save "#{number}.pdf" unless ENV['DYNO']

# as an alternative to saving the file, try sending the raw PDF stream:
pdf.to_pdf

end

In case the issue persists, could you attach the whole of the error message? - I'd like to know which method raises the exception.

@jordanmichaelrushing
Copy link
Author

PERFECT! Idk why I didn't think to try parse. Fyi for Heroku the saving wasn't an issue. Here's my final code for future reference for any future googlers who maybe have the same issue..

def email_pdf(email_address, number)
  company_logo = CombinePDF.new("logo.pdf").pages[0]
  pdf = CombinePDF.parse open("https://patentimages.storage.googleapis.com/pdfs/#{number}.pdf").read
  pdf.pages.each {|page| page << company_logo}
  pdf.save "#{number}.pdf"
  Email.pdf_email(email_address,number).deliver
  File.delete("#{number}.pdf")
end

@boazsegev
Copy link
Owner

Thanks for the update, I'm happy it worked out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants