This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Sending PDF as an email attachment on Rails

TylerGarlick wrote on 2011-06-01:
I have an app which I want to after I generate a pdf attach it to an email.

Using the sample code:

client = Pdfcrowd::Client.new("username", "apikey")
pdf = client.convertURI('http://example.com')

Do I need to do anything to that pdf to make it work for an attachment because when I just used the pdf variable as the attachment I got a blank pdf with nothing in it.

Any thoughts?

Thanks,

Tyler
TylerGarlick wrote on 2011-06-01:
The best case scenario would be to save the pdf to a S3 bucket, then I could just use s3 to pull down the file. What I'm trying to avoid is to save it to disk and just load it in memory or some sort of stream.
support wrote on 2011-06-01:
Hi Tyler,

If you run this code
client = Pdfcrowd::Client.new("*", "*") 
pdf = client.convertURI('http://example.com') 
print pdf.length

what does it print? It should be a non-zero number.

If you write it to a file, can you open it in a PDF viewer?
f = File.new("example_com.pdf", "wb")
f.write(pdf)
f.close


As for the S3, do you mean that the API would return a URL pointing to the generated PDF instead of the PDF file itself?
TylerGarlick wrote on 2011-06-01:
Essentially what i'd like to do is save it to a memory stream, and send the memory stream to S3. The main complication I have getting the pdf to a stream of some sort. Is there a way to create a stream without saving it to the disk?
support wrote on 2011-06-01:
The convertURI() method returns a String instance which contains the generated PDF. You can use it without saving it to a file.

Another option is to pass an object that implements write() method as the second argument to convertURI():
class MyStream
  def write(chunk)
    # process the chunk
  end
end

client = Pdfcrowd::Client.new("*", "*") 
mystream = MyStream.new()
client.convertURI('http://example.com', mystream)

The write() method is called for each received chunk.

Does this help?