Ruby ruport practice-fpdf of Chinese PDF report

Source: Internet
Author: User

In ruport, The pdf_writer does not support Chinese characters, and the output Chinese characters are garbled. I checked a lot of information online and did not find a good solution. I had to check the source code. Why does the PDF tool provided by ruport not support Chinese output.

Ruport: formatter: PDF: writer find the following code:

Metrics = load_font_metrics (font)

Metrics = PDF: writer: fontmetrics. Open (font)

PDF: writer: fontmetrics: metrics_path = [file. Join (file. dirname (file. expand_path (_ file _), 'fonts')]

 

Original metrics_path = % ruby_home %/lib/Ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/Writer/Fonts

If the path does not support Chinese characters, the system starts to add a *. ini font file that supports Chinese characters. The helpless file is found, put it in this path, and an error is reported during running (maybe the file is not supported by pai_writer)

 

Abandon this method and turn the target to fpdf

1) download fpdf http://download.csdn.net/source/5608

2) decompress the package to the Lib of the rails project.

3) Follow Ruby roport practices-a simple report system to complete ruport applications

4) Next, modify reportoutputcontroller. RB.

Modify class ruport: formatter: PDF to the following content:

Class ruport: formatter: fpdf <ruport: formatter: PDF <br/> def using _writer <br/> unless @ using _writer <br/> @ using _writer =: fpdf. new <br/> @ pai_writer.extend (pdf_chinese) <br/> @ pai_writer.addpage <br/> @ override <br/> @ pai_writer.setfont ('gb', '', options [: font_size] | 16) <br/> end <br/> return @ pai_writer <br/> end </P> <p> def render_pdf <br/> output <pai_writer.output <br/> end </P> <p> end <br/> class PDF <ruport:: formatter: fpdf <br/> renders: PDF,: For => basesqlcontroller <br/> build: data_sheet DO <br/> eval (options [: Invalid Content]) <br/> end

 

5) modify the content of template_content in the data table report_templates

Pai_writer.setfont ('gb', 'B', 10) <br/> pai_writer.setleftmargin (80) <br/> pai_writer.cell (40, 10, iconv. conv ('gb2312', 'utf-8', 'product report 2') <br/> pai_writer.ln () <br/> pai_writer.setfont ('gb ','', 8) <br/> pai_writer.cell (, iconv. conv ('gb2312', 'utf-8', 'name'), 'C') <br/> pai_writer.cell (, iconv. conv ('gb2312', 'utf-8', 'category '), 'C') <br/> pai_writer.cell (, iconv. conv ('gb2312', 'utf-8', 'price'), 1, 0, 'C') <br/> pai_writer.ln () <br/> data. each do | p | <br/> pai_writer.cell (, iconv. conv ('gb2312', 'utf-8', P. title), 1) <br/> pai_writer.cell (40, 5, iconv. conv ('gb2312', 'utf-8', P. description), 1) <br/> pai_writer.cell (40, 5, p. price. to_s, 1) <br/> pai_writer.ln () <br/> end

 

Report results:

 

 

 

 

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.