gdelugre / origami

Origami is a pure Ruby library to parse, modify and generate PDF documents.
GNU Lesser General Public License v3.0
326 stars 110 forks source link

Can I get a buffer or a string of the PDF? #41

Closed andy-twosticks closed 6 years ago

andy-twosticks commented 6 years ago

I'd like to be able to generate a PDF and print it without saving it as a file. Perhaps it's just me, but I can't see how to do that?

In the code I can see PDF#output, but it's a private method, so I don't want to use that.

gdelugre commented 6 years ago

Yes you can. The write method accepts any kind of IO object.

strio = StringIO.new
PDF.new.write(strio)

p strio.string
# "%PDF-1.0\n1 0 obj\n<<\n\t/Pages 2 0 R\n\t/Type /Catalog\n>>\nendobj\n2 0 obj\n<<\n\t/Count 1\n\t/Kids [3 0 R]\n\t/Type /Pages\n>>\nendobj\n3 0 obj\n<<\n\t/Parent 2 0 R\n\t/Resources <<\n\t\t/Font <<\n\t\t\t/F1 <<\n\t\t\t\t/Type /Font\n\t\t\t\t/Subtype /Type1\n\t\t\t\t/BaseFont /Helvetica\n\t\t\t>>\n\t\t>>\n\t>>\n\t/Type /Page\n\t/MediaBox [0 0 595 842]\n>>\nendobj\nxref\n0 4\n0000000000 65535 f \n0000000009 00000 n \n0000000060 00000 n \n0000000120 00000 n \ntrailer\n<<\n\t/Root 1 0 R\n\t/Size 4\n>>\nstartxref\n306\n%%EOF\n"