Commit 5e9e01bf authored by Jean-Philippe Lang's avatar Jean-Philippe Lang

missing rfpdf in the repository...

git-svn-id: http://redmine.rubyforge.org/svn/trunk@49 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent f7df1b7d
1.00 Added view template functionality
1.10 Added Chinese support
1.11 Added Japanese support
1.12 Added Korean support
1.13 Updated to fpdf.rb 1.53d.
Added makefont and fpdf_eps.
Handle \n at the beginning of a string in MultiCell.
Tried to fix clipping issue in MultiCell - still needs some work.
1.14 2006-09-26
* Added support for @options_for_rfpdf hash for configuration:
* Added :filename option in this hash
If you're using the same settings for @options_for_rfpdf often, you might want to
put your assignment in a before_filter (perhaps overriding :filename, etc in your actions).
Copyright (c) 2006 4ssoM LLC <www.4ssoM.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
= RFPDF Template Plugin
A template plugin allowing the inclusion of ERB-enabled RFPDF template files.
== Example .rb method Usage
In the controller, something like:
def mypdf
pdf = FPDF.new()
#
# Chinese
#
pdf.extend(PDF_Chinese)
pdf.AddPage
pdf.AddBig5Font
pdf.SetFont('Big5','',18)
pdf.Write(5, '²{®É®ð·Å 18 C Àã«× 83 %')
icBig5 = Iconv.new('Big5', 'UTF-8')
pdf.Write(15, icBig5.iconv("宋体 should be working"))
send_data pdf.Output, :filename => "something.pdf", :type => "application/pdf"
end
== Example .rfdf Usage
In the controller, something like:
def mypdf
@options_for_rfpdf ||= {}
@options_for_rfpdf[:file_name] = "nice_looking.pdf"
end
In the layout (make sure this is the only item in the layout):
<%= @content_for_layout %>
In the view (mypdf.rfpdf):
<%
pdf = FPDF.new()
#
# Chinese
#
pdf.extend(PDF_Chinese)
pdf.AddPage
pdf.AddBig5Font
pdf.SetFont('Big5','',18)
pdf.Write(5, '²{®É®ð·Å 18 C Àã«× 83 %')
icBig5 = Iconv.new('Big5', 'UTF-8')
pdf.Write(15, icBig5.iconv("宋体 should be working"))
#
# Japanese
#
pdf.extend(PDF_Japanese)
pdf.AddSJISFont();
pdf.AddPage();
pdf.SetFont('SJIS','',18);
pdf.Write(5,'9ÉñåéÇÃåˆäJÉeÉXÉgÇåoǃPHP 3.0ÇÕ1998îN6åéÇ…åˆéÆÇ…ÉäÉäÅ[ÉXÇ≥ÇÍNjǵÇΩÅB');
icSJIS = Iconv.new('SJIS', 'UTF-8')
pdf.Write(15, icSJIS.iconv("これはテキストである should be working"))
#
# Korean
#
pdf.extend(PDF_Korean)
pdf.AddUHCFont();
pdf.AddPage();
pdf.SetFont('UHC','',18);
pdf.Write(5,'PHP 3.0Àº 1998³â 6¿ù¿¡ °ø½ÄÀûÀ¸·Î ¸±¸®ÁîµÇ¾ú´Ù. °ø°³ÀûÀÎ Å×½ºÆ® ÀÌÈľà 9°³¿ù¸¸À̾ú´Ù.');
icUHC = Iconv.new('UHC', 'UTF-8')
pdf.Write(15, icUHC.iconv("이것은 원본 이다"))
#
# English
#
pdf.AddPage();
pdf.SetFont('Arial', '', 10)
pdf.Write(5, "should be working")
%>
<%= pdf.Output() %>
== Configuring
You can configure Rfpdf by using an @options_for_rfpdf hash in your controllers.
Here are a few options:
:filename (default: action_name.pdf)
Filename of PDF to generate
Note: If you're using the same settings for @options_for_rfpdf often, you might want to
put your assignment in a before_filter (perhaps overriding :filename, etc in your actions).
== Problems
Layouts and partials are currently not supported; just need
to wrap the PDF generation differently.
require 'rfpdf'
ActionView::Base::register_template_handler 'rfpdf', RFPDF::View
\ No newline at end of file
# Copyright (c) 2006 4ssoM LLC <www.4ssoM.com>
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rfpdf/errors'
require 'rfpdf/view'
require 'rfpdf/fpdf'
require 'rfpdf/rfpdf'
require 'rfpdf/chinese'
require 'rfpdf/japanese'
require 'rfpdf/korean'
# Translation of the bookmark class from the PHP FPDF script from Olivier Plathey
# Translated by Sylvain Lafleur and ?? with the help of Brian Ollenberger
#
# First added in 1.53b
#
# Usage is as follows:
#
# require 'fpdf'
# require 'bookmark'
# pdf = FPDF.new
# pdf.extend(PDF_Bookmark)
#
# This allows it to be combined with other extensions, such as the Chinese
# module.
module PDF_Bookmark
def PDF_Bookmark.extend_object(o)
o.instance_eval('@outlines,@OutlineRoot=[],0')
super(o)
end
def Bookmark(txt,level=0,y=0)
y=self.GetY() if y==-1
@outlines.push({'t'=>txt,'l'=>level,'y'=>y,'p'=>self.PageNo()})
end
def putbookmarks
@nb=@outlines.size
return if @nb==0
lru=[]
level=0
@outlines.each_index do |i|
o=@outlines[i]
if o['l']>0
parent=lru[o['l']-1]
# Set parent and last pointers
@outlines[i]['parent']=parent
@outlines[parent]['last']=i
if o['l']>level
# Level increasing: set first pointer
@outlines[parent]['first']=i
end
else
@outlines[i]['parent']=@nb
end
if o['l']<=level and i>0
# Set prev and next pointers
prev=lru[o['l']]
@outlines[prev]['next']=i
@outlines[i]['prev']=prev
end
lru[o['l']]=i
level=o['l']
end
# Outline items
n=@n+1
@outlines.each_index do |i|
o=@outlines[i]
newobj
out('<</Title '+(textstring(o['t'])))
out('/Parent '+(n+o['parent']).to_s+' 0 R')
if o['prev']
out('/Prev '+(n+o['prev']).to_s+' 0 R')
end
if o['next']
out('/Next '+(n+o['next']).to_s+' 0 R')
end
if o['first']
out('/First '+(n+o['first']).to_s+' 0 R')
end
if o['last']
out('/Last '+(n+o['last']).to_s+' 0 R')
end
out(sprintf('/Dest [%d 0 R /XYZ 0 %.2f
null]',1+2*o['p'],(@h-o['y'])*@k))
out('/Count 0>>')
out('endobj')
end
# Outline root
newobj
@OutlineRoot=@n
out('<</Type /Outlines /First '+n.to_s+' 0 R')
out('/Last '+(n+lru[0]).to_s+' 0 R>>')
out('endobj')
end
def putresources
super
putbookmarks
end
def putcatalog
super
if not @outlines.empty?
out('/Outlines '+@OutlineRoot.to_s+' 0 R')
out('/PageMode /UseOutlines')
end
end
end
This diff is collapsed.
module RFPDF
class GenerationError < StandardError #:nodoc:
end
end
\ No newline at end of file
This diff is collapsed.
# Information
#
# PDF_EPS class from Valentin Schmidt ported to ruby by Thiago Jackiw (tjackiw@gmail.com)
# working for Mingle LLC (www.mingle.com)
# Release Date: July 13th, 2006
#
# Description
#
# This script allows to embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
# Only vector drawing is supported, not text or bitmap. Although the script was successfully
# tested with various AI format versions, best results are probably achieved with files that
# were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2).
#
# ImageEps(string file, float x, float y [, float w [, float h [, string link [, boolean useBoundingBox]]]])
#
# Same parameters as for regular FPDF::Image() method, with an additional one:
#
# useBoundingBox: specifies whether to position the bounding box (true) or the complete canvas (false)
# at location (x,y). Default value is true.
#
# First added to the Ruby FPDF distribution in 1.53c
#
# Usage is as follows:
#
# require 'fpdf'
# require 'fpdf_eps'
# pdf = FPDF.new
# pdf.extend(PDF_EPS)
# pdf.ImageEps(...)
#
# This allows it to be combined with other extensions, such as the bookmark
# module.
module PDF_EPS
def ImageEps(file, x, y, w=0, h=0, link='', use_bounding_box=true)
data = nil
if File.exists?(file)
File.open(file, 'rb') do |f|
data = f.read()
end
else
Error('EPS file not found: '+file)
end
# Find BoundingBox param
regs = data.scan(/%%BoundingBox: [^\r\n]*/m)
regs << regs[0].gsub(/%%BoundingBox: /, '')
if regs.size > 1
tmp = regs[1].to_s.split(' ')
@x1 = tmp[0].to_i
@y1 = tmp[1].to_i
@x2 = tmp[2].to_i
@y2 = tmp[3].to_i
else
Error('No BoundingBox found in EPS file: '+file)
end
f_start = data.index('%%EndSetup')
f_start = data.index('%%EndProlog') if f_start === false
f_start = data.index('%%BoundingBox') if f_start === false
data = data.slice(f_start, data.length)
f_end = data.index('%%PageTrailer')
f_end = data.index('showpage') if f_end === false
data = data.slice(0, f_end) if f_end
# save the current graphic state
out('q')
k = @k
# Translate
if use_bounding_box
dx = x*k-@x1
dy = @hPt-@y2-y*k
else
dx = x*k
dy = -y*k
end
tm = [1,0,0,1,dx,dy]
out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm',
tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]))
if w > 0
scale_x = w/((@x2-@x1)/k)
if h > 0
scale_y = h/((@y2-@y1)/k)
else
scale_y = scale_x
h = (@y2-@y1)/k * scale_y
end
else
if h > 0
scale_y = $h/((@y2-@y1)/$k)
scale_x = scale_y
w = (@x2-@x1)/k * scale_x
else
w = (@x2-@x1)/k
h = (@y2-@y1)/k
end
end
if !scale_x.nil?
# Scale
tm = [scale_x,0,0,scale_y,0,@hPt*(1-scale_y)]
out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm',
tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]))
end
data.split(/\r\n|[\r\n]/).each do |line|
next if line == '' || line[0,1] == '%'
len = line.length
# next if (len > 2 && line[len-2,len] != ' ')
cmd = line[len-2,len].strip
case cmd
when 'm', 'l', 'v', 'y', 'c', 'k', 'K', 'g', 'G', 's', 'S', 'J', 'j', 'w', 'M', 'd':
out(line)
when 'L':
line[len-1,len]='l'
out(line)
when 'C':
line[len-1,len]='c'
out(line)
when 'f', 'F':
out('f*')
when 'b', 'B':
out(cmd + '*')
end
end
# restore previous graphic state
out('Q')
Link(x,y,w,h,link) if link
end
end
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
# Copyright (c) 2006 4ssoM LLC <www.4ssoM.com>
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Thanks go out to Bruce Williams of codefluency who created RTex. This
# template handler is modification of his work.
#
# Example Registration
#
# ActionView::Base::register_template_handler 'rfpdf', RFpdfView
module RFPDF
class View
def initialize(action_view)
@action_view = action_view
# Override with @options_for_rfpdf Hash in your controller
@options = {
# Run through latex first? (for table of contents, etc)
:pre_process => false,
# Debugging mode; raises exception
:debug => false,
# Filename of pdf to generate
:file_name => "#{@action_view.controller.action_name}.pdf",
# Temporary Directory
:temp_dir => "#{File.expand_path(RAILS_ROOT)}/tmp"
}.merge(@action_view.controller.instance_eval{ @options_for_rfpdf } || {}).with_indifferent_access
end
def render(template, local_assigns = {})
@pdf_name = "Default.pdf" if @pdf_name.nil?
unless @action_view.controller.headers["Content-Type"] == 'application/pdf'
@generate = true
@action_view.controller.headers["Content-Type"] = 'application/pdf'
@action_view.controller.headers["Content-disposition:"] = "inline; filename=\"#{@options[:file_name]}\""
end
assigns = @action_view.assigns.dup
if content_for_layout = @action_view.instance_variable_get("@content_for_layout")
assigns['content_for_layout'] = content_for_layout
end
result = @action_view.instance_eval do
assigns.each do |key,val|
instance_variable_set "@#{key}", val
end
local_assigns.each do |key,val|
class << self; self; end.send(:define_method,key){ val }
end
ERB.new(template).result(binding)
end
end
end
end
\ No newline at end of file
#!/usr/bin/env ruby
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment