Prawn Gem: How to Create the .Pdf from an *Existing* File (.Xls)

Prawn gem: How to create the .pdf from an *existing* file (.xls)

I'd suggest that you break the problem down.

  1. Can you read xls with Ruby? Possibly, but it's flaky at best. However, you can easily read csv, and xls exports nicely to that format.

  2. Can you write a 'table' of values to a prawn pdf? Yes

So, (almost) all you need is a little program that can parse a csv file into a prawn-friendly table-structure and then hand it off to Prawn for generation.

How to generate PDF that is returned to the client using the Prawn gem

Try in the controller:

respond_to do |format|
format.pdf do
`send_data your_pdf.render, :filename=>"default_filename.pdf", :type=>"application/pdf"`
end
end

where your_pdf is the object you created with Prawn.

Ruby on Rails: Plugins/gems for converting a .xls file to a .pdf file?

I don't know of anything that will do it without shelling out some cash. You might be able to roll your own by combining the roo gem with the nice swanky pdfkit gem. The roo gem would allow you to read the contents of the excel file. You would then need to construct an html document and pass that to pdfkit which converts html to pdf. That is a little indirect, but should get the job done.

How to edit or write on existing PDF with Ruby?

you have to definitely check out Prawn gem, by which you can generate any custom pdf files. You can actually use prawn to write in text into existing pdfs by treating the existing PDF as a template for your new Prawn document.

For example:

filename = "#{Prawn::DATADIR}/pdfs/multipage_template.pdf"
Prawn::Document.generate("full_template.pdf", :template => filename) do
text "THis content is written on the first page of the template", :align => :center
end

This will write text onto the first page of the old pdf.

See more here:
http://prawn.majesticseacreature.com/manual.pdf

Creating Microsoft Word (.docx) documents in Ruby

As has been noted, there don't appear to be any libraries to manipulate Open XML documents in Ruby, but OpenXML Developer has complete documentation on the format of Open XML documents.

If what you want is to send a copy of a standard document (like a form letter) customized for each user, it should be fairly simple given that a DOCX is a ZIP file that contains various parts in a directory hierarchy. Have a DOCX "template" that contains all the parts and tree structure that you want to send to all users (with no real content), then simply create new (or modify existing) pieces that contain the user-specific content you want and inject it into the ZIP (DOCX file) before sending it to the user.

For example: You could have document-template.xml that contains Dear [USER-PLACEHOLDER]:. When a user requests the document, you replace [USER-PLACEHOLDER] with the user's name, then add the resulting document.xml to the your-template.docx ZIP file (which would contain all the images and other parts you want in the Word document) and send that resulting document to the user.

Note that if you rename a .docx file to .zip it is trivial to explore the structure and format of the parts inside. You can remove or replace images or other parts very easily with any ZIP manipulation tools or programmatically with code.

Generating a brand new Word document with completely custom content from raw XML would be very difficult without access to an API to make the job easier. If you really need to do that, you might consider installing Mono, then use VB.NET, C# or IronRuby to create your Open XML documents using the Open XML Format SDK 1.0. Since you would just be using the Microsoft.Office.DocumentFormat.OpenXml.Packaging Namespace to manipulate Open XML documents, it should work okay in Mono, which seems to support everything the SDK requires.



Related Topics



Leave a reply



Submit