Oct 24

Just quick post how to use acts_as_flying_saucer with rails 3.1 and Heroku.

I was testing acts_as_flying_saucer with rails 3.1.It is working fine so far.
but when i have added external style sheet it was hanging on dev mode.
and later on i figure out it is external style sheet causing an issue.
So i precomplied css and then try to generate pdf it is working.
Now it is time to test on heroku.
I am using cedar stack and ruby 1.9.2 after precomplied step pdf.
By default pdf is generated on system /tmp directory.
In heroku we can set application tmp path to generate pdf i.e ./tmp

I was testing acts_as_flying_saucer with rails 3.1.It is working fine so far,but when i have added external style sheet it was hanging on dev mode rails is hanging

After litte bit of googling  i figure out it is external style sheet causing an issue because of asset pipeline and thread issue for serving static file mainly in dev mode.

So i simply pre complied css or we can manually (actual full path) or manipulate asset path .after this PDF is generated properly.

Now it is time to test on Heroku.I am using cedar stack and ruby 1.9.2 after pre complied step.

Now point tmp directory to application tmp directory.

ActsAsFlyingSaucer::Config.options = {:tmp_path => "./tmp"}
Tagged with:
Oct 24
It’s about more than 6 months i haven’t written any post.
Why ?
Recently join new organiztion,
Can’t get time because we are working almost more than 10 to 12 hr day with messy code.
So we are spending more time to refactoring and optimising code,and scaling application also.
Now i started making my time to writing.
In my current organization we are generating various Pdf report.
We are using prawn but now we are switching to  acts_as_flying_saucer.
So there are couple of challenges i have faced
1) XHTML code is not proper
2) Special character in html
Solution
Cleanup Html code
I am using tidy library clean up html code.
By passing :clean=>true it will cleanup html befor generating pdf.
3) Send PDF as email
Solution
Generate PDF on server and do some processing like attached to mail.
:send_attachment => true it will generate pdf and return path of pdf file then do some processing.
So couple of new feature is added to acts_as_flying_saucer
1) Clean up  html by passing :clean=>true
2) Generate pdf on server (locally) by passing :send_attachment=>true
3) acts_as_flying_saucer is also working with rails 3.1 (precomplie asset) and heroku.

It’s about more than 6 months i haven’t written any post Why ?

because recently join new organization, and can’t get much more time , we are working almost more than 10 to 12 hr day with messy code.So we are spending more time to  refactoring and optimizing code,and scaling application.😥

In my current organization we are generating various PDF report.We are using prawn but now we are switching to acts_as_flying_saucer.

So there are couple of challenges i have faced

  1. XHTML code is not proper
  2. Special character in html

Solution: Cleanup Html code

Continue reading »

Tagged with:
May 12

In previous article we have seen that how pdf is generated  using act_as_flying_saucer plugin.

Following code snippets is used for generating pdf with bookmark .


<html>
  <head>

    <bookmarks>
      <bookmark <strong>name="Section 1" href="#section_1"</strong>>
        <bookmark name="Section 1.1" href="#section_11"></bookmark>
        <bookmark name="Section 1.2" href="#section_12"></bookmark>
      </bookmark>
      <bookmark name="Section 2" href="#section_2"></bookmark>
      <bookmark name="Section 2" href="#section_3"></bookmark>
    </bookmarks>

  </head>

  <body>

    <div style="page-break-before: always;">
      <a <strong>name="section_1"</strong>>Section 1</a>
    </div>
    <div style="page-break-before: always;">
      <a name="section_11">Section 1.1</a>
    </div>
    <div style="page-break-before: always;">
      <a name="section_12">Section 1.1.2</a>
    </div>
    <div style="page-break-before: always;">
       <a name="section_2">Section 2</a>
    </div>
    <div style="page-break-before: always;">
       <a name="section_3">Section 3</a>
    </div>

  </body>

</html>

<bookmark name=”section 1″  href=”#section_1″> tag is used to generate bookmark.
which contains name attribute for displaying name of bookmark.
href attribute is used for navigation purpose.

<bookmark> tag can be nested so it can be used for generating nested bookmark
<bookmark> tag is wrapper with <bookmarks> tag. This tag placed in html header.

Now in html body whichever anchor tag has name attribute with value as same as bookmark
href value on that position user is navigated.

Mainly two tag is used which is displayed below.

<bookmarks>
<bookmark name=”Name Of Bookmark”  href=”#bookmark1“>< /bookmark>
</bookmarks>
<a name=”bookmark1″>Text</a>

Tagged with:
May 12

Pdf with password protected in rails

In previous article we have seen that how pdf is generated  using act_as_flying_saucer plugin. Now  act_flying_saucer  has added support for  password protected pdf.

To generate PDF with password protection just pass :password=>”xxx” to render_pdf method.

render_pdf :file=>'pdf/generate_pdf.html.erb',:password=>"xxx"</pre>

You can install act_as_flying_saucer plugin

 script/plugin install git@github.com:amardaxini/acts_as_flying_saucer.git

For  further details you can visit article or visit http://github.com/amardaxini/act_as_flying_saucer

Tagged with:
Jan 19

Rails Pdf Plugin act_as_flying_saucer

There are various ways to generate pdf documents in any language.In Rails we can use prawn library ,HtmlDoc,PrinceXml and many other library,using their api we can generate pdf document.Basically the primary goal is converting HTML web pages to PDF Document,without much changing existing CSS and HTML.

Using the Flying Saucer Project we can achieve this.we can convert HTML + CSS to PDF documents without much changing HTML and CSS. It also support CSS 2 and many properties of CSS 3 for printing Header,Footer,Page Number,Paginated Tables and many more.This Project is built on Java so Java is required on system.

Lets Start How to generate PDF Using Flying Saucer.

 script/plugin install git://github.com/amardaxini/acts_as_flying_saucer.git

This Plugin forked From

http://github.com/dagi3d/acts_as_flying_saucer

Which has older version of flying saucer project.

Next Step after installing plugin add flying_saucer.rb file at config/initializers.


ActsAsFlyingSaucer::Config.options = {
:java_bin => "/usr/bin/java",          # java binary
:classpath_separator => ':',  # classpath separator. unixes system use ':' and windows ';'
:tmp_path => "/tmp",          # path where temporary files will be stored
}

After Setting Java path , classpath separator and tmp path now do following step.

class PdfController < ActionController::Base
  acts_as_flying_saucer

  def generate_pdf
    render_pdf :template => 'pdf/pdf_template'
  end
end
  render_pdf :file=>'pdf/generate_pdf.html.erb'
  render_pdf :template=>'pdf/generate_pdf.pdf.erb',
             :send_file => { :filename => 'pdfdoc.pdf' }

Add act_as_flying_saucer to controller then render_pdf.There are various ways to render pdf using “template” or “file“,:send_file option use to send pdf file to client side.if we are using any external css file then this css shoulda have media option as ‘print’.


<%= stylesheet_link_tag 'pdf' ,:media=>'print' %>

If pdf containing images which generate on the fly.so instad of using

<%= image_tag %>

use

 <img src=" " >

Before rendering PDF validate HTML,like whether all tags are close properly or not.otherwise it gives an parse error.

Now You are ready to Generate PDF. Hurray!!!!!!!!!

How to generate Header, Footer,Page Number and Automation Of HTML Validation will be discussed in next post.

Tagged with:
preload preload preload