On 4月23日, 午後4:03, Syysk <syysk2
...@gmail.com> wrote:
> PDFで文字化けについて
> general_csv_encoding: CP932
> general_pdf_encoding: CP932
確かに、こちらの方が良さそうではありますね。とりあえずは SJIS にしました。
> 解決策など知っている方おられますでしょうか?
とりあえずは、手元では以下のような修正で直っているように見えます。
(手元はsyysk氏と同じような環境です。)
ちなみに、vendor/plugins/rfpdf/ 以下をいくら修正しても、
上流側(元々の Ruby FPDF 側)で反映されない限り、
取り込んでもらえないように思うので、app/helper/ifpdf_helper.rb 側を編集してます。
ただし、特異メソッドの MultiCell() をオーバーライドできずに、
MultiCell2() としている所がダサく、やむを得ず app/views/issues/ 以下も
合わせていじっていますし、日本語の事しか考えてないので、
いろいろ副作用があるかもしれません。
もっとうまい方法がある気がするので、だれか教えて下さい。
ついでに、プロパティのタイトルの文字化けも修正してあります。
(SetTitle() と textstring() をオーバーライド)。
--
Hajime BABA
=======================================================================
Index: app/helpers/ifpdf_helper.rb
===================================================================
--- app/helpers/ifpdf_helper.rb (revision 1401)
+++ app/helpers/ifpdf_helper.rb (working copy)
@@ -56,6 +56,17 @@
end
def Cell(w,h=0,txt='',border=0,ln=0,align='',fill=0,link='')
+ txt = convUTF8toSJISandEscape0x5c(txt)
+ super w,h,txt,border,ln,align,fill,link
+ end
+
+ def MultiCell2(w,h,txt,border=0,align='L',fill=0)
+ txt = convUTF8toSJISandEscape0x5c(txt)
+ MultiCell(w,h,txt,border,align,fill)
+ end
+
+ def convUTF8toSJISandEscape0x5c(txt)
+ txt << " "
@ic ||= Iconv.new(l(:general_pdf_encoding), 'UTF-8')
# these quotation marks are not correctly rendered in the pdf
txt = txt.gsub(/[“”]/, '"') if txt
@@ -67,9 +78,8 @@
rescue
txt
end || ''
- super w,h,txt,border,ln,align,fill,link
end
-
+
def Footer
SetFont(@font_for_footer, 'I', 8)
SetY(-15)
@@ -79,7 +89,31 @@
SetX(-30)
Cell(0, 5, PageNo().to_s + '/{nb}', 0, 0, 'C')
end
-
+
+ def SetTitle(txt)
+ case current_language.to_s
+ when 'ja', 'zh', 'zh-tw'
+ txt = begin
+ utf16txt = Iconv.conv('UTF-16BE', 'UTF-8', txt)
+ hextxt = "<FEFF" # FEFF is BOM
+ hextxt << utf16txt.unpack("C*").map {|x|
sprintf("%02X",x) }.join
+ hextxt << ">"
+ rescue
+ txt
+ end || ''
+ end
+ super(txt)
+ end
+
+ def textstring(s)
+ # Format a text string
+ if s =~ /^</ # This means the string is hex-dumped.
+ return s
+ else
+ return '('+escape(s)+')'
+ end
+ end
+
end
end
Index: app/views/issues/index.rfpdf
===================================================================
--- app/views/issues/index.rfpdf (revision 1401)
+++ app/views/issues/index.rfpdf (working copy)
@@ -42,9 +42,9 @@
pdf.Cell(30, row_height, issue.priority.name, 0, 0,
'L', 1)
pdf.Cell(40, row_height, issue.assigned_to ?
issue.assigned_to.n
ame : '', 0, 0, 'L', 1)
pdf.Cell(25, row_height,
format_date(issue.updated_on), 0, 0, 'L
', 1)
- pdf.MultiCell(0, row_height, (@project ==
issue.project ? issue.
subject : "#{issue.project.name} - #{issue.subject}"))
+ pdf.MultiCell2(0, row_height, (@project ==
issue.project ? issue
.subject : "#{issue.project.name} - #{issue.subject}"))
pdf.Line(10, pdf.GetY, 287, pdf.GetY)
pdf.SetY(pdf.GetY() + 1)
end
%>
-<%= pdf.Output %>
\ No newline at end of file
+<%= pdf.Output %>
Index: app/views/issues/_pdf.rfpdf
===================================================================
--- app/views/issues/_pdf.rfpdf (revision 1401)
+++ app/views/issues/_pdf.rfpdf (working copy)
@@ -48,7 +48,7 @@
pdf.SetFontStyle('B',9)
pdf.Cell(35,5, custom_value.custom_field.name + ":","L")
pdf.SetFontStyle('',9)
- pdf.MultiCell(155,5, (show_value custom_value),"R")
+ pdf.MultiCell2(155,5, (show_value custom_value),"R")
end
pdf.SetFontStyle('B',9)
@@ -60,7 +60,7 @@
pdf.SetFontStyle('B',9)
pdf.Cell(35,5, l(:field_description) + ":")
pdf.SetFontStyle('',9)
- pdf.MultiCell(155,5, issue.description,"BR")
+ pdf.MultiCell2(155,5, issue.description,"BR")
pdf.Line(pdf.GetX, y0, pdf.GetX, pdf.GetY)
pdf.Line(pdf.GetX, pdf.GetY, 170, pdf.GetY)
@@ -77,7 +77,7 @@
pdf.Ln
unless changeset.comments.blank?
pdf.SetFontStyle('',8)
- pdf.MultiCell(190,5,
changeset.comments)
+ pdf.MultiCell2(190,5,
changeset.comments)
end
pdf.Ln
end
@@ -97,7 +97,7 @@
end
if journal.notes?
pdf.SetFontStyle('',8)
- pdf.MultiCell(190,5, journal.notes)
+ pdf.MultiCell2(190,5, journal.notes)
end
pdf.Ln
end
Index: lang/ja.yml
===================================================================
--- lang/ja.yml (revision 1401)
+++ lang/ja.yml (working copy)
@@ -50,7 +50,7 @@
general_lang_name: 'Japanese (日本語)'
general_csv_separator: ','
general_csv_encoding: SJIS
-general_pdf_encoding: UTF-8
+general_pdf_encoding: SJIS
general_day_names: 月曜日,火曜日,水曜日,木曜日,金曜日,土曜日,日曜日
general_first_day_of_week: '7'
=========================================================================== =======