Nesting pdfs - Prawn - ruby-on-rails

Some context: I have some html.erb files I use to create a pdf using WickedPdf that I can make their own pdf or I can render in another pdf, but I'm switching to Prawn because Prawn::Table is more predictable with page breaks than WickedPdf.
I've got a few POROs that inherit from a BasePdf class that handles all the boilerplate pdf stuff like our company's header, font size, and access to the #document etc. Is it possible to have a Prawn pdf PORO that can be a pdf on its own like the rest of my Prawn pdfs but also be included in another pdf in the middle of a page (as opposed to starting a new page).

Not sure about prawn, but here https://github.com/igorkasyanchuk/rails_pdf/blob/master/docs/report_4.png an example of PDF with a table.
Also on the second page of PDF, it will contain table header which is good.

We use extensively Prawn with plain Ruby or Rails to build complex business reports.
You can use all Ruby goodness to build reports like modules, class inheritance and custom methods. I thing this is one of Prawn's best part!
Each one of them can print/redner a part of a page or used as some kind of utility method. You actually right plain ruby that renders to pdf.
So you can have a base class with your company's lay out and custom modules which print some part of every specific report. Each module can take a hash with all required parameters for that module to render. This way you can also decouple reports from the rest of the app (that is db access etc.) and use or test them independently.

Related

Rails PDF generation in model

I am writing and invoicing application in Ruby On Rails, which is supposed to generate a PDF from a model so called out_invoice to an in_invoice under certain conditions.
The PDF for the out_invoice is already separately generated on demand using wicked_pdf gem basically having a HTML template and sending back the PDF inside the action and downloading it.
I have successfully programmed the duplication inside the out_invoice model which works fine for all values of the out_invoice. Including an attached PDF in a separate attribute.
But instead of generating the PDF and attaching it to the in_invoice. It fails. Some Controller/Model MVC thing I assume!
The PDF for the out_invoice is currently generated separately in a controller action so I tried copying the code: I tried rendering the template inside the model which seems to work fine, but "sending" is not defined for Rails models. If I just return the WickedPDF without sending it I get an error: ActiveSupport::MessageVerifier::InvalidSignature
Must be something super simple I am forgetting + I have no clue what is the reason for the error. I am not a front-end guy so don't want to use something else as the already existing HTML template for rendering. But worst case I write the template newly in another engine. Looked at Prawn but this means rewriting everything.
Can somebody help? Thanks!

How to add templates to rails-latex?

I have recently started working with rails-latex on my Rails application. I would like to use a template when generating a pdf, but don't see any way to do so mentioned in the Github readme. Is there some sort of directory or method to adding LaTeX templates?
You're looking for the LatexToPdf.generate_pdf method.
It takes in two arguments:
latex content
a configuration hash
...and returns the pdf binary, which you'll need to write to a file.
Regarding the tex content, I have a directory with latex templates and would read in them using the File class, then pass in the content as an argument.
I suggest reading through the source to if you need to add configuration.
Note that under the hood, the rails-latex (LatexToPdf) gem still depends on a TeX extension (which you'll need to download) to generate the pdf. The default is pdflatex, and I've personally used xelatex.

generate PDF from html document in Rails

anyone knows about some gem or app in rails to create pdf docs from html5 and css3 with some client side programming??
Thanks ;)
Thank you for your responses. I need this for a possible project wich will use jquery for box positioning and then, will export the resultant html to pdf. Roughly, ¿is this possible?
PDFKit and WickedPDF for Client side generation, as suggested by Raphael and jcadam
Flying Saucer with JRuby: http://xhtmlrenderer.java.net
Personally, I got more accomplished by using Prawn PDF generator. It's capabilities are much more extensive, IMHO.
Prawn for Ruby PDF generation: https://github.com/prawnpdf/prawn
WickedPDF: https://github.com/mileszs/wicked_pdf
I'm using it now and rather like it:
"Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked take care of the hard stuff."
If you need more control, also check out Prawn: https://github.com/prawnpdf/prawn. Though Prawn is not an HTML -> PDF converter and is more useful for creating PDFs from scratch.
I'm using both Prawn and WickedPDF on my current project: WickedPDF to generate 'printable' versions of web pages, and Prawn for generating detailed PDF reports.
I believe the PDFKit gem may be what you're looking for.
https://github.com/pdfkit/PDFKit
There's also IMGKit if you want images instead of PDFs.
https://github.com/csquared/IMGKit

handling wiki links with markdown in ruby

I'm building a simple app with Rails using Markdown for storing content. My question is how to build internal [[wiki]] style links? Either by pre-processing before they get to markdown or some markdown derivative? I release I could probably preprocess using regex, but I'm guessing there are others with ready built solutions.
For example I know Instiki uses both markdown and [[wiki|Wiki]] links and I've looked but couldn't figure out how they're handling it.
Any tips?
If you are using the redcarpet gem you can either use a preprocessor or you can modifiy the generated HTML output.
Have a look at How to extend Redcarpet to support a media library. This article shows how to convert image references to custom HTML and also how to replace boilerplate identifiers with the actual content.
I guess both approaches could be adapted for your specific problem:
The renderer approach directly manipulates the generated HTML code from the markdown code. (This is more elegant as you are not messing with Markdown code)
The preprocess approach manipulates the code by using regular expressions (as you already mentioned) (This is more flexible, but also a little bit messy)

Checking CSS within a rails controller or in plain ruby?

I need to take a database text field and parse it for
duplication and garbage
malice
whitelisted selectors
compress and output as a css file
Since there might be a rails way I'm unaware or something ready made I'm asking before I waste time trying to reinvent a wheel. My searching revealed nothing, mostly in rails seems aimed at view level, and css seems to be an unattended niche in this area (plenty of html though).
I'm aware of the sanitize gem (doesn't do css immediately, yet another thing I'd need to map out and code) and the built in rails stuff (not a lot of tutorial, aimed mostly at the view level). I need a gem, lib, module or something similar that I can work with in a controller or queue.
EDIT:
Without getting too deep into the specifics of the project: administrative users can add css for their portions of the site. As part of the flow I'm going to save the raw css and then process and save the processed css. The db stuff is archival mostly, the css file is output immediately. Because there is few places to add modified css and only admins have access to the css, it sort of works but I'm looking to make it more robust in the future where admins who may not be as conversant with the security needs or not as css aware can operate.
The most basic example is that it just a text field on an admin page. The admin cuts and pastes css there, submits, and the application turns it into a css file that gets included with the designated pages, which works because the current admins know the application, the css of the application, and what they can and cannot change. The goal is to make this more robust for future admins who might not be as savvy.
To simply sanitize CSS, you can use the SanitizeHelper built into Rails: http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-sanitize_css
Have you looked at Sass? It has all of the parsing logic built in, for a superset of CSS. You could add a feature (Sass support) and save yourself the need to parse/validate the CSS all in one go.
You can generate output CSS from Sass (or just plain CSS, since Sass [with the SCSS syntax] is a fully-backward-compatible superset of CSS) like this:
output_css = Sass::Engine.new(sass_content, :syntax => :scss).render
There are a bunch of options that you'll probably want to look into at http://sass-lang.com/
Another option is Less. The new Twitter Bootstrap framework uses Less, and Rails 3.1 uses Sass. The biggest difference is that the official Less parser/compiler is built in JavaScript, so you could actually validate and compile in the user's browser while they work and show them any errors before they save. Of course then you need to run a JavaScript engine (e.g. V8) in your Rails application if you want to use Less to validate the incoming CSS still.

Resources