Ruby ruport practice-Prawn of a Chinese PDF report

Source: Internet
Author: User

Fpdf supports Chinese PDF printing, but there are many disappointments. According to my current usage, I have summarized the following points:

1) The pdf_chinese font provided by fpdf writes the GB font into the Code. If we want to introduce other Chinese fonts, it is not easy to use.

2) The numbers output by fpdf are not as beautiful as ruport: formatter: font of PDF.

3) fpdf does not provide many methods. For general report printing, the most commonly used method may be the cell method, however, it would be too inconvenient to print a common report that requires code to control line breaks, draw lines, and write cells.

 

Prawn is also a ruby PDF report tool that is highly popular. It varies from person to person as to who is better than fpdf. fpdf is a PDF library written in PHP, prawn is not prawn: it is simpler to use Ruby to generate PDF.

 

Next let's take a look at how prawn can print PDF files (for basic implementation, refer to Ruby ruport practices-simple report system)

1. Install Prawn

Gem install Prawn

 

Ii. Modify enviroment. Rb

Add the following reference:

Require "Prawn" <br/> require 'prawn/layout'

 

3. Introduce appropriate Chinese Fonts

Prawn supports external introduction of Chinese fonts, such as ""

Copy the font files (*. TTF) supported by the local machine from C:/Windows/fonts

% Ruby_home %/lib/Ruby/gems/1.8/gems/prawn-core-0.8.4/data/Fonts

 

In the code

@ Maid "# {:: prawn: basedir}/data/fonts/simkai. TTF"
Font to be referenced

 

4. Modify reportoutputcontroller. Rb

Replace PDF with the following code: <ruport: formatter: PDF:

Class ruport: formatter: prawn <ruport: formatter: PDF <br/> def required _writer <br/> unless @ required _writer <br/> @ required _writer =: prawn:: document. new <br/> @ pai_writer.font "# {:: prawn: basedir}/data/fonts/simkai. TTF "<br/> end <br/> return @ pai_writer <br/> end <br/> def render_pdf <br/> output <pai_writer.render <br/> end <br /> end </P> <p> class PDF <ruport:: formatter: prawn <br/> renders: PDF,: For => reportoutputcontroller <br/> build: data_sheet DO <br/> eval (options [: outputcontent]) <br/> end

 

5. Save repoert_templates

The content of template_content is saved as follows:

Pai_writer.text ("product list", <br/>: align =>: Center,: size => 15) <br/> pai_writer.image ("Public/images/ruport.jpg ", <br/>: At => [0,730], <br/>: Height => 50, <br/>: width => 100) <br/> pheader = % W [category name price] <br/> pdata = [] <br/> for I in 0 .. data. length-1 do <br/> temp = [DATA [I]. description, data [I]. title, data [I]. price. to_s] <br/> pdata [I] = temp <br/> end <br/> pai_writer.table (pdata, <br/>: font_size => 10, <br/>: headers => pheader, <br/>: vertical_padding => 2, <br/>: horizontal_padding => 5, <br/>: Position =>: Center)

 

Demo effect:

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.