prawnto is a plugin for rails that you can use to generate PDFs from within your webapp using prawn. Wow! try saying that fast a couple times.
Here’s the situation. In my application I have to generate work orders for the work to be done and the materials needed to preform the work. There are times when the person may want to generate the work order and then save it for later, and other times where they will generate the work order and print it out. In the first case I generate the work order and then save it to scribd. In the other case, I just generate the work order and display it to the screen as a PDF. Since I want to keep the application DRY I want to use the same file to generate the file and save it as I do when I will just display it. However there is no easy way to do this without using some of the ‘dsl’ features in prawnto.
So, I’ve been playing with the application, prawn and prawnto in order to generate the PDF and I’d made some progress until I got the the point where I actually had to pass the information from the models into the template. After searching and looking around on the web I decided that I need to just read the code. I discovered that the demos are outdated.
Notice that the controller looks like this:
def dsl_hash @y = 'world' prawnto :dsl=>[:x=>'hello',:y=>@y] end
Instead the code should look like this instead.
def dsl_hash
@y = 'world'
prawnto :dsl=>{:x=>'"hello"',:y=>'@y'}
end
Notice how everything is sent in a hash and is quoted. Subtle, but it took a while to track down. Even the “” around the string literal isn’t in the test and I just had to experiment to see what worked. But work it does.
So, with just a couple subtle changes, now everything works and the code can move forward. Next: Figuring out how to make it work with prawn (independent of prawnto), while keeping the view code unchanged. I’ve made some steps and I’m doing stuff with the following code…now I just need to figure out how to pass the variables.
source_file = File::open(filename);
source = source_file.read
Prawn::Document.generate(resulting_filename) do |pdf|
pdf.instance_eval(source, filename)
end
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.