rbpdf-fruitcake0525 1.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +96 -0
  3. data/Gemfile +7 -0
  4. data/LICENSE.TXT +504 -0
  5. data/README.md +64 -0
  6. data/Rakefile +12 -0
  7. data/lib/core/rmagick.rb +93 -0
  8. data/lib/htmlcolors.rb +207 -0
  9. data/lib/rbpdf.rb +15138 -0
  10. data/lib/rbpdf/version.rb +3 -0
  11. data/lib/unicode_data.rb +18315 -0
  12. data/logo_example.png +0 -0
  13. data/rbpdf.gemspec +37 -0
  14. data/test/err_font1.rb +3 -0
  15. data/test/err_font2.rb +4 -0
  16. data/test/logo_rbpdf_8bit.gif +0 -0
  17. data/test/logo_rbpdf_8bit.jpg +0 -0
  18. data/test/logo_rbpdf_8bit.png +0 -0
  19. data/test/logo_rbpdf_8bit_alpha.gif +0 -0
  20. data/test/logo_rbpdf_mono_gray.jpg +0 -0
  21. data/test/logo_rbpdf_mono_gray.png +0 -0
  22. data/test/logo_rbpdf_mono_rgb.jpg +0 -0
  23. data/test/logo_rbpdf_mono_rgb.png +0 -0
  24. data/test/png_test_alpha.png +0 -0
  25. data/test/png_test_msk_alpha.png +0 -0
  26. data/test/png_test_non_alpha.png +0 -0
  27. data/test/rbpdf_bidi_test.rb +438 -0
  28. data/test/rbpdf_bookmark_test.rb +56 -0
  29. data/test/rbpdf_cell_test.rb +266 -0
  30. data/test/rbpdf_content_test.rb +196 -0
  31. data/test/rbpdf_css_test.rb +636 -0
  32. data/test/rbpdf_dom_test.rb +338 -0
  33. data/test/rbpdf_font_func_test.rb +41 -0
  34. data/test/rbpdf_font_style_test.rb +33 -0
  35. data/test/rbpdf_font_test.rb +291 -0
  36. data/test/rbpdf_format_test.rb +26 -0
  37. data/test/rbpdf_func_test.rb +135 -0
  38. data/test/rbpdf_html_anchor_test.rb +107 -0
  39. data/test/rbpdf_html_func_test.rb +166 -0
  40. data/test/rbpdf_html_test.rb +580 -0
  41. data/test/rbpdf_htmlcell_test.rb +64 -0
  42. data/test/rbpdf_image_rmagick_test.rb +294 -0
  43. data/test/rbpdf_image_test.rb +107 -0
  44. data/test/rbpdf_test.rb +377 -0
  45. data/test/rbpdf_transaction_test.rb +199 -0
  46. data/test/rbpdf_viewerpreferences_test.rb +37 -0
  47. data/test/rbpdf_write_test.rb +225 -0
  48. data/test/test_helper.rb +4 -0
  49. data/test_unicode.rbpdf +115 -0
  50. data/utf8test.txt +120 -0
  51. metadata +205 -0
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class RbpdfFormatTest < Test::Unit::TestCase
4
+ test "set_page_orientation" do
5
+ pdf = RBPDF.new
6
+
7
+ pagedim = pdf.set_page_orientation('')
8
+ assert_equal pagedim['or'], 'P'
9
+ assert_equal pagedim['pb'], true
10
+ assert_equal pagedim['olm'], nil
11
+ assert_equal pagedim['orm'], nil
12
+ assert_in_delta pagedim['bm'], 20, 0.1
13
+
14
+ pagedim = pdf.set_page_orientation('P')
15
+ assert_equal pagedim['or'], 'P'
16
+
17
+ pagedim = pdf.set_page_orientation('L', false)
18
+ assert_equal pagedim['or'], 'L'
19
+ assert_equal pagedim['pb'], false
20
+
21
+ pagedim = pdf.set_page_orientation('P', true, 5)
22
+ assert_equal pagedim['or'], 'P'
23
+ assert_equal pagedim['pb'], true
24
+ assert_equal pagedim['bm'], 5
25
+ end
26
+ end
@@ -0,0 +1,135 @@
1
+ require 'test_helper'
2
+
3
+ class RbpdfTest < Test::Unit::TestCase
4
+ class MYPDF < RBPDF
5
+ def getSpaceString
6
+ super
7
+ end
8
+ end
9
+
10
+ test "get_html_unit_to_units test" do
11
+ pdf = RBPDF.new
12
+ unit = pdf.get_html_unit_to_units("100", 1)
13
+ assert_in_delta unit, 35.27, 0.01
14
+
15
+ unit = pdf.get_html_unit_to_units("100px", 1, 'px', false)
16
+ assert_in_delta unit, 35.27, 0.01
17
+
18
+ unit = pdf.get_html_unit_to_units(100, 1, 'pt', false)
19
+ assert_in_delta unit, 35.27, 0.01
20
+
21
+ unit = pdf.get_html_unit_to_units(100.0, 1, 'pt', false)
22
+ assert_in_delta unit, 35.27, 0.01
23
+
24
+ unit = pdf.get_html_unit_to_units("100.0", 1, 'pt', false)
25
+ assert_in_delta unit, 35.27, 0.01
26
+
27
+ unit = pdf.get_html_unit_to_units("200", 1, '%', false)
28
+ assert_equal unit, 2.0
29
+
30
+ unit = pdf.get_html_unit_to_units("400%", 1, '%', false)
31
+ assert_equal unit, 4.0
32
+
33
+ unit = pdf.get_html_unit_to_units("10", 1, '%', false)
34
+ assert_equal unit, 0.1
35
+
36
+ unit = pdf.get_html_unit_to_units("10mm", 1, '%', false)
37
+ assert_in_delta unit, 10, 0.01
38
+
39
+ unit = pdf.get_html_unit_to_units("10", 1, 'mm', false)
40
+ assert_in_delta unit, 10, 0.01
41
+
42
+ unit = pdf.get_html_unit_to_units(10, 1, 'mm', false)
43
+ assert_in_delta unit, 10, 0.01
44
+
45
+ unit = pdf.get_html_unit_to_units("1", 1, 'cm', false)
46
+ assert_in_delta unit, 10, 0.01
47
+
48
+ unit = pdf.get_html_unit_to_units(10, 1, 'em', false)
49
+ assert_equal unit, 10
50
+
51
+ unit = pdf.get_html_unit_to_units(10, 2, 'em', false)
52
+ assert_equal unit, 20
53
+ end
54
+
55
+ test "getSpaceString test" do
56
+ pdf = MYPDF.new
57
+ spacestr = pdf.getSpaceString()
58
+ assert_equal spacestr, 32.chr
59
+
60
+ pdf.set_font('freesans', '', 18)
61
+ spacestr = pdf.getSpaceString()
62
+ assert_equal spacestr, 0.chr + 32.chr
63
+ end
64
+
65
+ test "revstrpos test" do
66
+ pdf = RBPDF.new
67
+ pos = pdf.revstrpos('abcd efgh ', 'cd')
68
+ assert_equal pos, 2
69
+
70
+ pos = pdf.revstrpos('abcd efgh ', 'cd ')
71
+ assert_equal pos, 2
72
+
73
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'cd')
74
+ assert_equal pos, 12
75
+
76
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'zy')
77
+ assert_equal pos, nil
78
+ end
79
+
80
+ test "revstrpos offset test 1" do
81
+ pdf = RBPDF.new
82
+
83
+ pos = pdf.revstrpos('abcd efgh ', 'cd', 3) # 'abc'
84
+ assert_equal pos, nil
85
+
86
+ pos = pdf.revstrpos('abcd efgh ', 'cd', 4) # 'abcd'
87
+ assert_equal pos, 2
88
+
89
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'cd', 3) # 'abc'
90
+ assert_equal pos, nil
91
+
92
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'cd', 4) # 'abcd'
93
+ assert_equal pos, 2
94
+
95
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'cd', 13) # 'abcd efgh abc'
96
+ assert_equal pos, 2
97
+
98
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'cd', 14) # 'abcd efgh abcd'
99
+ assert_equal pos, 12
100
+ end
101
+
102
+ test "revstrpos offset test 2" do
103
+ pdf = RBPDF.new
104
+
105
+ pos = pdf.revstrpos('abcd efgh ', 'cd', -6) # 'abcd'
106
+ assert_equal pos, 2
107
+
108
+ pos = pdf.revstrpos('abcd efgh ', 'cd', -7) # 'abc'
109
+ assert_equal pos, nil
110
+
111
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'cd', -4) # 'abcd efgh abcd'
112
+ assert_equal pos, 12
113
+
114
+ pos = pdf.revstrpos('abcd efgh abcd efg', 'cd', -5) # 'abcd efgh abc'
115
+ assert_equal pos, 2
116
+ end
117
+
118
+ test "set_line_style Basic test" do
119
+ pdf = RBPDF.new
120
+
121
+ pdf.set_line_style({'width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => [0, 0, 0]})
122
+ pdf.set_line_style({'width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => '', 'phase' => 0, 'color' => [255, 0, 0]})
123
+ pdf.set_line_style({'width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => '1,2,3,4', 'phase' => 0, 'color' => [255, 0, 0]})
124
+ pdf.set_line_style({'width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => 'a', 'phase' => 0, 'color' => [255, 0, 0]}) # Invalid
125
+ end
126
+
127
+ test "get_string_width encoding test" do
128
+ return unless 'test'.respond_to?(:force_encoding)
129
+
130
+ pdf = RBPDF.new
131
+ str = 'test'.force_encoding('UTF-8')
132
+ width = pdf.get_string_width(str)
133
+ assert_equal str.encoding.to_s, 'UTF-8'
134
+ end
135
+ end
@@ -0,0 +1,107 @@
1
+ require 'test_helper'
2
+
3
+ class RbpdfTest < Test::Unit::TestCase
4
+ class MYPDF < RBPDF
5
+ def getPageBuffer(page)
6
+ super
7
+ end
8
+ end
9
+
10
+ test "anchor with text inside" do
11
+ pdf = RBPDF.new
12
+ pdf.add_page()
13
+
14
+ htmlcontent = '<a name="foo">HTML Example</a>'
15
+ pdf.write_html(htmlcontent, true, 0, true, 0)
16
+
17
+ position = pdf.get_html_anchor_position('foo')
18
+ assert_equal position, [1, 10.001249999999999]
19
+ end
20
+
21
+ test "anchor with id" do
22
+ pdf = RBPDF.new
23
+ pdf.add_page()
24
+
25
+ htmlcontent = '<a id="foo">HTML Example</a>'
26
+ pdf.write_html(htmlcontent, true, 0, true, 0)
27
+
28
+ position = pdf.get_html_anchor_position('foo')
29
+ assert_equal position, [1, 10.001249999999999]
30
+ end
31
+
32
+ test "empty anchor" do
33
+ pdf = RBPDF.new
34
+ pdf.add_page()
35
+
36
+ htmlcontent = '<a id="foo"></a>'
37
+ pdf.write_html(htmlcontent, true, 0, true, 0)
38
+
39
+ position = pdf.get_html_anchor_position('foo')
40
+ assert_equal position, [1, 10.001249999999999]
41
+ end
42
+
43
+
44
+ test "anchor with overtical offset" do
45
+ pdf = RBPDF.new
46
+ pdf.add_page()
47
+
48
+ htmlcontent = '<br><br><br><br><br><br><br><br><br><br><a id="foo"></a>'
49
+ pdf.write_html(htmlcontent, true, 0, true, 0)
50
+
51
+ position = pdf.get_html_anchor_position('foo')
52
+ assert_equal position, [1, 57.626249999999985]
53
+ end
54
+
55
+
56
+ test "on the second page" do
57
+ pdf = RBPDF.new
58
+ pdf.add_page()
59
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
60
+ pdf.write_html(htmlcontent, true, 0, true, 0)
61
+
62
+ htmlcontent = '<a id="foo"></a>'
63
+ pdf.write_html(htmlcontent, true, 0, true, 0)
64
+
65
+ position = pdf.get_html_anchor_position('foo')
66
+ assert_equal position, [3, 68.20958333333331]
67
+ end
68
+
69
+
70
+ test "maps when anchor after link" do
71
+ pdf = RBPDF.new
72
+ pdf.add_page()
73
+
74
+ htmlcontent = '<a href="#foo">FooLink</a>'
75
+ pdf.write_html(htmlcontent, true, 0, true, 0)
76
+
77
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
78
+ pdf.write_html(htmlcontent, true, 0, true, 0)
79
+
80
+ htmlcontent = '<a id="foo"></a>'
81
+ pdf.write_html(htmlcontent, true, 0, true, 0)
82
+
83
+ pdf.send(:mapLinksToHtmlAnchors)
84
+ link_position = pdf.instance_variable_get(:@links)[1]
85
+ assert_equal link_position, [3, 73.50124999999998]
86
+ end
87
+
88
+ test "maps when anchor before link" do
89
+ pdf = RBPDF.new
90
+ pdf.add_page()
91
+
92
+ htmlcontent = '<a id="foo"></a>'
93
+ pdf.write_html(htmlcontent, true, 0, true, 0)
94
+
95
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
96
+ pdf.write_html(htmlcontent, true, 0, true, 0)
97
+
98
+ htmlcontent = '<a href="#foo">FooLink</a>'
99
+ pdf.write_html(htmlcontent, true, 0, true, 0)
100
+
101
+ pdf.send(:mapLinksToHtmlAnchors)
102
+
103
+ link_position = pdf.instance_variable_get(:@links)[1]
104
+ assert_equal link_position, [1, 10.001249999999999]
105
+ end
106
+
107
+ end
@@ -0,0 +1,166 @@
1
+ require 'test_helper'
2
+
3
+ class RbpdfTest < Test::Unit::TestCase
4
+ class MYPDF < RBPDF
5
+ def addHTMLVertSpace(hbz, hb, cell, firstorlast)
6
+ super
7
+ end
8
+ def sanitize_html(html)
9
+ super
10
+ end
11
+ end
12
+
13
+ test "html func addHTMLVertSpace empty lines test" do
14
+ pdf = MYPDF.new
15
+ pdf.add_page()
16
+
17
+ # same line, start position
18
+ x1 = pdf.get_x
19
+ pdf.set_x(x1 + 10)
20
+ y1 = pdf.get_y
21
+ pdf.addHTMLVertSpace(1, 0, false, true)
22
+ x2 = pdf.get_x
23
+ y2 = pdf.get_y
24
+ assert_equal x2, x1
25
+ assert_equal y2, y1
26
+
27
+ # same line, @c_margin position
28
+ margins = pdf.get_margins
29
+
30
+ x1 = pdf.get_x
31
+ y1 = pdf.get_y
32
+ pdf.addHTMLVertSpace(1, 0, true, true)
33
+ x2 = pdf.get_x
34
+ y2 = pdf.get_y
35
+ assert_equal x2, x1 + margins['cell']
36
+ assert_equal y2, y1
37
+ end
38
+
39
+ test "html func addHTMLVertSpace add line test" do
40
+ pdf = MYPDF.new
41
+ pdf.add_page()
42
+
43
+ # next line, start position
44
+ x1 = pdf.get_x
45
+ pdf.set_x(x1 + 10)
46
+ y1 = pdf.get_y
47
+ pdf.addHTMLVertSpace(5, 0, false, false)
48
+ x2 = pdf.get_x
49
+ y2 = pdf.get_y
50
+ assert_equal x2, x1
51
+ assert_equal y2, y1 + 5
52
+
53
+ # next line, @c_margin position
54
+ margins = pdf.get_margins
55
+
56
+ x1 = pdf.get_x
57
+ y1 = pdf.get_y
58
+ pdf.addHTMLVertSpace(5, 0, true, false)
59
+ x2 = pdf.get_x
60
+ y2 = pdf.get_y
61
+ assert_equal x2, x1 + margins['cell']
62
+ assert_equal y2, y1 + 5
63
+ end
64
+
65
+ test "html func addHTMLVertSpace height of the break test 1" do
66
+ pdf = MYPDF.new
67
+ pdf.add_page()
68
+
69
+ margins = pdf.get_margins
70
+ x1 = pdf.get_x
71
+ y1 = pdf.get_y
72
+ pdf.addHTMLVertSpace(0, 5, true, false) # height of the break : 5
73
+ x2 = pdf.get_x
74
+ y2 = pdf.get_y
75
+ assert_equal x2, x1 + margins['cell']
76
+ assert_equal y2, y1 + 5
77
+
78
+ pdf.addHTMLVertSpace(0, 5, true, false) # height of the break : 5
79
+ x3 = pdf.get_x
80
+ y3 = pdf.get_y
81
+ assert_equal x3, x2
82
+ assert_equal y3, y2
83
+
84
+ pdf.addHTMLVertSpace(0, 5 + 2, true, false) # height of the break : 7
85
+ x4 = pdf.get_x
86
+ y4 = pdf.get_y
87
+ assert_equal x4, x3
88
+ assert_equal y4, y3 + 2
89
+
90
+ pdf.addHTMLVertSpace(0, 5, true, false) # height of the break : 7
91
+ x5 = pdf.get_x
92
+ y5 = pdf.get_y
93
+ assert_equal x5, x4
94
+ assert_equal y5, y4
95
+
96
+ pdf.addHTMLVertSpace(0, 5 + 2 + 1, true, false) # height of the break : 8
97
+ x6 = pdf.get_x
98
+ y6 = pdf.get_y
99
+ assert_equal x6, x5
100
+ assert_equal y6, y5 + 1
101
+
102
+ pdf.addHTMLVertSpace(0, 10, true, true) # height of the break : 0 (reset)
103
+ x7 = pdf.get_x
104
+ y7 = pdf.get_y
105
+ assert_equal x7, x6
106
+ assert_equal y7, y6
107
+
108
+ pdf.addHTMLVertSpace(0, 2, true, false) # height of the break : 2
109
+ x8 = pdf.get_x
110
+ y8 = pdf.get_y
111
+ assert_equal x8, x7
112
+ assert_equal y8, y7 + 2
113
+ end
114
+
115
+ test "html func addHTMLVertSpace height of the break test 2" do
116
+ pdf = MYPDF.new
117
+ pdf.add_page()
118
+
119
+ x1 = pdf.get_x
120
+ y1 = pdf.get_y
121
+ pdf.addHTMLVertSpace(10, 5, false, false) # height of the break : 5
122
+ x2 = pdf.get_x
123
+ y2 = pdf.get_y
124
+ assert_equal x2, x1
125
+ assert_equal y2, y1 + 10 + 5
126
+
127
+ pdf.addHTMLVertSpace(10, 5, false, false) # height of the break : 5
128
+ x3 = pdf.get_x
129
+ y3 = pdf.get_y
130
+ assert_equal x3, x2
131
+ assert_equal y3, y2 + 10
132
+
133
+ pdf.addHTMLVertSpace(10, 5 + 2, false, false) # height of the break : 7
134
+ x4 = pdf.get_x
135
+ y4 = pdf.get_y
136
+ assert_equal x4, x3
137
+ assert_equal y4, y3 + 10 + 2
138
+ end
139
+
140
+ test "html func sanitize test 1" do
141
+ pdf = MYPDF.new
142
+ pdf.add_page()
143
+ html = '<table border="1"><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead><tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr><tr><td>' + 'ABC' + '</td></tr></table>'
144
+ html = pdf.sanitize_html(html).gsub(/[\r\n]/,'')
145
+
146
+ assert_equal html, %{<table border="1"><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead><tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr><tr><td>ABC</td></tr></table>}
147
+ end
148
+
149
+ test "html func sanitize test 2" do
150
+ pdf = MYPDF.new
151
+ pdf.add_page()
152
+
153
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
154
+ html = '<table cellpadding="1"><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead><tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr><tr><td>' + htmlcontent + '</td></tr></table>'
155
+ html = pdf.sanitize_html(html).gsub(/[\r\n]/,'')
156
+ assert_equal html, %{<table cellpadding="1"><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead><tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr><tr><td>1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br></td></tr></table>}
157
+ end
158
+
159
+ test "html func sanitize open angled bracket '<' test" do
160
+ pdf = MYPDF.new
161
+ pdf.add_page()
162
+ html = "<p>AAA '<'-BBB << <<< '</' '<//' '<///' <</ <<// CCC.</p>"
163
+ html = pdf.sanitize_html(html).gsub(/[\r\n]/,'')
164
+ assert_equal %{<p>AAA '&lt;'-BBB &lt;&lt; &lt;&lt;&lt; '&lt;/' '&lt;//' '&lt;///' &lt;&lt;/ &lt;&lt;// CCC.</p>}, html
165
+ end
166
+ end
@@ -0,0 +1,580 @@
1
+ # coding: ASCII-8BIT
2
+ require 'test_helper'
3
+
4
+ class RbpdfHtmlTest < Test::Unit::TestCase
5
+ class MYPDF < RBPDF
6
+ def getPageBuffer(page)
7
+ super
8
+ end
9
+
10
+ # get text count and x_pos from pdf page
11
+ def get_html_text_position_x(page, regrep_text, x_pos_exp=nil)
12
+ count_line, count_text, x_pos, y_pos = get_html_text_position(page, regrep_text, x_pos_exp)
13
+ return count_line, count_text, x_pos
14
+ end
15
+
16
+ # get text count and y_pos from pdf page
17
+ def get_html_text_position_y(page, regrep_text)
18
+ count_line, count_text, x_pos, y_pos = get_html_text_position(page, regrep_text)
19
+ return count_line, count_text, y_pos
20
+ end
21
+
22
+ # get text count and pos from pdf page
23
+ def get_html_text_position(page, regrep_text, x_pos_exp=nil)
24
+ content = []
25
+ contents = getPageBuffer(page)
26
+ contents.each_line {|line| content.push line.chomp }
27
+ count_line = count_text = 0
28
+ x_pos = y_pos = -1
29
+ content.each do |line|
30
+ count_line += 1 if line =~ /TJ ET Q$/ # Text Line Count
31
+ if line =~ regrep_text
32
+ count_text += 1
33
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
34
+ x_pos = $1
35
+ y_pos = $2 if y_pos == -1 # y first position only
36
+
37
+ if x_pos.nil? or y_pos.nil? # Error
38
+ return count_line, count_text, nil, nil
39
+ end
40
+ if !x_pos_exp.nil? and x_pos != x_pos_exp # Error
41
+ return count_line, count_text, x_pos, y_pos
42
+ end
43
+ end
44
+ end
45
+ return count_line, count_text, x_pos, y_pos
46
+ end
47
+
48
+ # get text from pdf page
49
+ def get_html_text(page)
50
+ content = []
51
+ contents = getPageBuffer(page)
52
+ contents.each_line {|line| content.push line.chomp }
53
+ pdf_text = ''
54
+ content.each do |line|
55
+ if line =~ /\[\((.*)\)\] TJ ET/
56
+ pdf_text << $1
57
+ end
58
+ end
59
+ return pdf_text
60
+ end
61
+ end
62
+
63
+ test "write_html Basic test" do
64
+ pdf = RBPDF.new
65
+ pdf.add_page()
66
+
67
+ htmlcontent = '<h1>HTML Example</h1>'
68
+ pdf.write_html(htmlcontent, true, 0, true, 0)
69
+
70
+ htmlcontent = 'abcdefghijklmnopgrstuvwxyz01234567890 abcdefghijklmnopgrstuvwxyz01234567890 abcdefghijklmnopgrstuvwxyz01234567890 abcdefghijklmnopgrstuvwxyz01234567890 abcdefghijklmnopgrstuvwxyz01234567890'
71
+ pdf.write_html(htmlcontent, true, 0, true, 0)
72
+
73
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
74
+ pdf.write_html(htmlcontent, true, 0, true, 0)
75
+
76
+ pno = pdf.get_page
77
+ assert_equal pno, 3
78
+ end
79
+
80
+ test "write_html Table test 1" do
81
+ pdf = RBPDF.new
82
+ pdf.add_page()
83
+
84
+ tablehtml = '<table border="1" cellspacing="1" cellpadding="1"><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>'
85
+ pdf.write_html(tablehtml, true, 0, true, 0)
86
+
87
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
88
+
89
+ tablehtml = '<table border="1" cellspacing="1" cellpadding="1"><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>' + htmlcontent + '</td></tr></table>'
90
+ pdf.write_html(tablehtml, true, 0, true, 0)
91
+
92
+ pno = pdf.get_page
93
+ assert_equal pno, 3
94
+ end
95
+
96
+ test "write_html Table test 2" do
97
+ pdf = MYPDF.new
98
+ pdf.add_page()
99
+
100
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
101
+
102
+ tablehtml = '<table border="1"><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr>
103
+ <tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr>
104
+ <tr><td>' + htmlcontent + '</td></tr></table>'
105
+ pdf.write_html(tablehtml, true, 0, true, 0)
106
+
107
+ pno = pdf.get_page
108
+ assert_equal pno, 3
109
+
110
+ # Page 1
111
+ count_line, count_text, xpos1 = pdf.get_html_text_position_x(1, /ABCD/) # Header
112
+ assert_not_nil xpos1
113
+ assert_equal 1, count_text
114
+ assert_equal 13, count_line
115
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(1, /abcd/)
116
+ assert_not_nil xpos2
117
+ assert_equal 1, count_text
118
+ assert_equal xpos1, xpos2
119
+ assert_equal 13, count_line
120
+
121
+ # Page 2
122
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /\([6-9]\)/, xpos1)
123
+ assert_not_nil xpos2
124
+ assert_equal xpos1, xpos2
125
+ assert_equal 7, count_line
126
+ end
127
+
128
+ test "write_html Table thead tag test 1" do
129
+ pdf = MYPDF.new
130
+ pdf.add_page()
131
+
132
+ tablehtml = '<table border="1" cellpadding="1" cellspacing="1">
133
+ <thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead>
134
+ <tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr>
135
+ </table>'
136
+
137
+ pdf.write_html(tablehtml, true, 0, true, 0)
138
+ page = pdf.get_page
139
+ assert_equal 1, page
140
+
141
+ count_line, count_text, xpos = pdf.get_html_text_position_x(1, /ABCD/) # Header
142
+ assert_equal 1, count_text
143
+ end
144
+
145
+ test "write_html Table thead tag test 2" do
146
+ pdf = MYPDF.new
147
+ pdf.add_page()
148
+
149
+ htmlcontent = '1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br><br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br><br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br><br><br><br><br><br><br><br><br>'
150
+
151
+ tablehtml = '<table><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead>
152
+ <tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr>
153
+ <tr><td>' + htmlcontent + '</td></tr></table>'
154
+
155
+ pdf.write_html(tablehtml, true, 0, true, 0)
156
+ page = pdf.get_page
157
+ assert_equal 3, page
158
+
159
+ # Page 1
160
+ count_line, count_text, xpos1 = pdf.get_html_text_position_x(1, /ABCD/) # Header
161
+ assert_not_nil xpos1
162
+ assert_equal 1, count_text
163
+ assert_equal 13, count_line
164
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(1, /abcd/)
165
+ assert_not_nil xpos2
166
+ assert_equal 1, count_text
167
+ assert_equal xpos1, xpos2
168
+ assert_equal 13, count_line
169
+
170
+ # Page 2
171
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /ABCD/, xpos1) # Header
172
+ assert_not_nil xpos2
173
+ assert_equal 1, count_text
174
+ assert_equal xpos1, xpos2
175
+ assert_equal 10, count_line
176
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /abcd/)
177
+ assert_equal 0, count_text
178
+ assert_equal 10, count_line
179
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /\([6-9]\)/, xpos1)
180
+ assert_not_nil xpos2
181
+ assert_equal xpos1, xpos2
182
+ assert_equal 10, count_line
183
+
184
+ # Page 3
185
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(3, /ABCD/, xpos1) # Header
186
+ assert_not_nil xpos2
187
+ assert_equal 1, count_text
188
+ assert_equal xpos1, xpos2
189
+ assert_equal 5, count_line
190
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(3, /abcd/)
191
+ assert_equal 0, count_text
192
+ assert_equal 5, count_line
193
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(3, /\(11\)/, xpos1)
194
+ assert_not_nil xpos2
195
+ assert_equal 1, count_text
196
+ assert_equal xpos1, xpos2
197
+ assert_equal 5, count_line
198
+ end
199
+
200
+ test "write_html_cell Table thead tag test" do
201
+ pdf = MYPDF.new
202
+ pdf.add_page()
203
+
204
+ htmlcontent = '<br>1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br>
205
+ <br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br>
206
+ <br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br>
207
+ <br><br><br><br><br><br><br><br>'
208
+
209
+ tablehtml ='<table><thead><tr>
210
+ <th style="text-align: left">Left align</th>
211
+ <th style="text-align: right">Right align</th>
212
+ <th style="text-align: center">Center align</th>
213
+ </tr> </thead><tbody> <tr>
214
+ <td style="text-align: left">left' + htmlcontent + '</td>
215
+ <td style="text-align: right">right</td>
216
+ <td style="text-align: center">center</td>
217
+ </tr> </tbody></table>'
218
+
219
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
220
+
221
+ page = pdf.get_page
222
+ assert_equal 1, page
223
+
224
+ # Page 1
225
+ count_line, count_text, xpos1 = pdf.get_html_text_position_x(1, /Left align/) # Header
226
+ assert_not_nil xpos1
227
+ assert_equal 1, count_text
228
+ assert_equal 13, count_line
229
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(1, /left/)
230
+ assert_not_nil xpos2
231
+ assert_equal 1, count_text
232
+ assert_equal 13, count_line
233
+ assert_equal xpos1, xpos2
234
+
235
+ # Page 2
236
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /Left align/, xpos1) # Header
237
+ assert_not_nil xpos2
238
+ assert_equal 1, count_text
239
+ assert_equal xpos1, xpos2
240
+ assert_equal 10, count_line
241
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /\(6\)/, xpos1)
242
+ assert_not_nil xpos2
243
+ assert_equal 1, count_text
244
+ assert_equal xpos1, xpos2
245
+ assert_equal 10, count_line
246
+
247
+ # Page 3
248
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(3, /Left align/, xpos1) # Header
249
+ assert_not_nil xpos2
250
+ assert_equal 1, count_text
251
+ assert_equal xpos1, xpos2
252
+ assert_equal 5, count_line
253
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(3, /\(11\)/, xpos1)
254
+ assert_not_nil xpos2
255
+ assert_equal 1, count_text
256
+ assert_equal xpos1, xpos2
257
+ assert_equal 5, count_line
258
+ end
259
+
260
+ test "write_html_cell Table thead tag cellpadding x position test" do
261
+ pdf = MYPDF.new
262
+ pdf.add_page()
263
+
264
+ htmlcontent = '<br>1<br><br><br><br><br><br><br><br><br><br> 2<br><br><br><br><br><br><br><br><br><br> 3<br><br><br><br><br><br><br><br><br><br> 4<br>
265
+ <br><br><br><br><br><br><br><br><br> 5<br><br><br><br><br><br><br><br><br><br> 6<br><br><br><br><br><br><br><br><br><br> 7<br><br><br><br><br><br><br>
266
+ <br><br><br> 8<br><br><br><br><br><br><br><br><br><br> 9<br><br><br><br><br><br><br><br><br><br> 10<br><br><br><br><br><br><br><br><br><br> 11<br><br>
267
+ <br><br><br><br><br><br><br><br>'
268
+
269
+ tablehtml ='<table cellpadding="10"><thead><tr>
270
+ <th style="text-align: left">Left align</th>
271
+ <th style="text-align: right">Center align</th>
272
+ <th style="text-align: left">Right align</th>
273
+ </tr> </thead><tbody> <tr>
274
+ <td style="text-align: left">left</td>
275
+ <td style="text-align: right">center</td>
276
+ <td style="text-align: left">right' + htmlcontent + '</td>
277
+ </tr> </tbody></table>'
278
+
279
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
280
+
281
+ page = pdf.get_page
282
+ assert_equal 1, page
283
+
284
+ # Page 1
285
+ count_line, count_text, xpos1 = pdf.get_html_text_position_x(1, /Right align/) # Header
286
+ assert_not_nil xpos1
287
+ assert_equal 1, count_text
288
+ assert_equal 13, count_line
289
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(1, /right/)
290
+ assert_not_nil xpos2
291
+ assert_equal 1, count_text
292
+ assert_equal xpos1, xpos2
293
+ assert_equal 13, count_line
294
+
295
+ # Page 2
296
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /Right align/, xpos1) # Header
297
+ assert_not_nil xpos2
298
+ assert_equal 1, count_text
299
+ assert_equal xpos1, xpos2
300
+ assert_equal 10, count_line
301
+ count_line, count_text, xpos2 = pdf.get_html_text_position_x(2, /\(6\)/, xpos1)
302
+ assert_not_nil xpos2
303
+ assert_equal 1, count_text
304
+ assert_equal xpos1, xpos2
305
+ assert_equal 10, count_line
306
+ end
307
+
308
+ test "write_html_cell Table thead tag cellpadding y position test 1" do
309
+ pdf = MYPDF.new
310
+ pdf.add_page()
311
+
312
+ table_start='<table cellpadding="10"><thead><tr>
313
+ <th style="text-align: left">Left align</th><th style="text-align: center">Center align</th><th style="text-align: right">Right align</th>
314
+ </tr></thead><tbody>'
315
+ table_col='<tr><td style="text-align: left">AAA</td><td style="text-align: center">BBB</td><td style="text-align: right">CCC</td></tr>'
316
+ table_end='</tbody></table>'
317
+ tablehtml= table_start + table_col * 30 + table_end
318
+
319
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
320
+
321
+ # Page 1
322
+ count_line, count_text, ypos1 = pdf.get_html_text_position_y(1, /Left align/) # Header
323
+ assert_not_nil ypos1
324
+ assert_equal 1, count_text
325
+ assert_equal 65, count_line
326
+ count_line, count_text, ypos2 = pdf.get_html_text_position_y(1, /AAA/)
327
+ assert_not_nil ypos2
328
+ assert_equal 20, count_text
329
+ assert_equal 65, count_line
330
+ base_pos = ypos1.to_i - ypos2.to_i
331
+
332
+ # Page 2
333
+ count_line, count_text, ypos1 = pdf.get_html_text_position_y(2, /Left align/) # Header
334
+ assert_not_nil ypos2
335
+ assert_equal 1, count_text
336
+ assert_equal 34, count_line
337
+ count_line, count_text, ypos2 = pdf.get_html_text_position_y(2, /AAA/)
338
+ assert_not_nil ypos2
339
+ assert_equal 10, count_text
340
+ assert_equal 34, count_line
341
+ assert_equal base_pos, ypos1.to_i - ypos2.to_i
342
+ end
343
+
344
+ test "write_html_cell Table thead tag cellpadding y position test 2" do
345
+ pdf = MYPDF.new
346
+ pdf.add_page()
347
+
348
+ table_start='abc<br><table cellpadding="10"><thead><tr>
349
+ <th style="text-align: left">Left align</th><th style="text-align: center">Center align</th><th style="text-align: right">Right align</th>
350
+ </tr></thead><tbody>'
351
+ table_col='<tr><td style="text-align: left">AAA</td><td style="text-align: center">BBB</td><td style="text-align: right">CCC</td></tr>'
352
+ table_end='</tbody></table>'
353
+ tablehtml= table_start + table_col * 30 + table_end
354
+
355
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
356
+
357
+ # Page 1
358
+ count_line, count_text, ypos1 = pdf.get_html_text_position_y(1, /Left align/) # Header
359
+ assert_not_nil ypos1
360
+ assert_equal 1, count_text
361
+ assert_equal 66, count_line
362
+ count_line, count_text, ypos2 = pdf.get_html_text_position_y(1, /AAA/)
363
+ assert_not_nil ypos2
364
+ assert_equal 20, count_text
365
+ assert_equal 66, count_line
366
+ base_pos = ypos1.to_i - ypos2.to_i
367
+
368
+ # Page 2
369
+ count_line, count_text, ypos1 = pdf.get_html_text_position_y(2, /Left align/) # Header
370
+ assert_not_nil ypos2
371
+ assert_equal 1, count_text
372
+ assert_equal 34, count_line
373
+ count_line, count_text, ypos2 = pdf.get_html_text_position_y(2, /AAA/)
374
+ assert_not_nil ypos2
375
+ assert_equal 10, count_text
376
+ assert_equal 34, count_line
377
+ assert_equal base_pos, ypos1.to_i - ypos2.to_i
378
+ end
379
+
380
+ test "write_html ASCII text test" do
381
+ pdf = MYPDF.new
382
+ pdf.add_page()
383
+
384
+ text = 'HTML Example'
385
+ htmlcontent = '<h1>' + text + '</h1>'
386
+ pdf.write_html(htmlcontent, true, 0, true, 0)
387
+ page = pdf.get_page
388
+ assert_equal 1, page
389
+
390
+ content = []
391
+ contents = pdf.getPageBuffer(1)
392
+ contents.each_line {|line| content.push line.chomp }
393
+
394
+ count_text = 0
395
+ content.each do |line|
396
+ count_text += 1 unless line.scan(text).empty?
397
+ end
398
+ assert_equal count_text, 1
399
+ end
400
+
401
+ test "write_html Non ASCII text test" do
402
+ pdf = MYPDF.new
403
+ pdf.add_page()
404
+
405
+ text = 'HTML Example ' + "\xc2\x83\xc2\x86"
406
+
407
+ htmlcontent = '<h1>' + text + '</h1>'
408
+ pdf.write_html(htmlcontent, true, 0, true, 0)
409
+ page = pdf.get_page
410
+ assert_equal 1, page
411
+
412
+ content = []
413
+ contents = pdf.getPageBuffer(1)
414
+ contents.each_line {|line| content.push line.chomp }
415
+
416
+ text = 'HTML Example ' + "\x83\x86"
417
+ text.force_encoding('ASCII-8BIT') if text.respond_to?(:force_encoding)
418
+ count_text = 0
419
+ content.each do |line|
420
+ line.force_encoding('ASCII-8BIT') if line.respond_to?(:force_encoding)
421
+ count_text += 1 unless line.scan(text).empty?
422
+ end
423
+ assert_equal count_text, 1
424
+ end
425
+
426
+ test "works internal links out of page range" do
427
+ pdf = RBPDF.new
428
+ pdf.add_page()
429
+
430
+ htmlcontent = '<a href="#100400_somelink">FooLink</a>'
431
+ pdf.write_html(htmlcontent, true, 0, true, 0)
432
+
433
+ assert_nothing_raised do
434
+ pdf.Close
435
+ end
436
+
437
+ assert_nothing_raised do
438
+ pdf.Output
439
+ end
440
+ end
441
+
442
+ test "write_html <b> tag test" do
443
+ pdf = MYPDF.new
444
+ pdf.set_print_header(false)
445
+ pdf.add_page()
446
+
447
+ text = ' ' + 'A' * 70
448
+ htmlcontent = '<b>' + text + '</b>'
449
+
450
+ pdf.write_html(htmlcontent, true, 0, true, 0)
451
+ pdf_text = pdf.get_html_text(1)
452
+ assert_equal 'A' * 70, pdf_text
453
+ end
454
+
455
+ test "write_html <i> tag test" do
456
+ pdf = MYPDF.new
457
+ pdf.set_print_header(false)
458
+ pdf.add_page()
459
+
460
+ text = ' ' + 'A' * 70
461
+ htmlcontent = '<i>' + text + '</i>'
462
+
463
+ pdf.write_html(htmlcontent, true, 0, true, 0)
464
+ pdf_text = pdf.get_html_text(1)
465
+ assert_equal 'A' * 70, pdf_text
466
+ end
467
+
468
+ test "write_html <u> tag test" do
469
+ pdf = MYPDF.new
470
+ pdf.set_print_header(false)
471
+ pdf.add_page()
472
+
473
+ text = ' ' + 'A' * 70
474
+ htmlcontent = '<u>' + text + '</u>'
475
+
476
+ pdf.write_html(htmlcontent, true, 0, true, 0)
477
+ pdf_text = pdf.get_html_text(1)
478
+ assert_equal 'A' * 70, pdf_text
479
+ end
480
+
481
+ test "write_html <pre> tag space 1 test" do
482
+ pdf = MYPDF.new
483
+ pdf.set_print_header(false)
484
+ pdf.add_page()
485
+
486
+ text = ' ' + 'A' * 70
487
+ htmlcontent = '<pre>' + text + '</pre>'
488
+
489
+ pdf.write_html(htmlcontent, true, 0, true, 0)
490
+ pdf_text = pdf.get_html_text(1)
491
+ assert_equal "\xa0" + 'A' * 70, pdf_text
492
+ end
493
+
494
+ test "write_html <pre> tag space 2 test" do
495
+ pdf = MYPDF.new
496
+ pdf.set_print_header(false)
497
+ pdf.add_page()
498
+
499
+ text = ' ' + 'A' * 70
500
+ htmlcontent = '<pre>' + text + '</pre>'
501
+
502
+ pdf.write_html(htmlcontent, true, 0, true, 0)
503
+ pdf_text = pdf.get_html_text(1)
504
+ assert_equal "\xa0" * 2 + 'A' * 70, pdf_text
505
+ end
506
+
507
+ test "write_html Character Entities test" do
508
+ pdf = MYPDF.new
509
+ pdf.set_print_header(false)
510
+
511
+ character_entities = {
512
+ '&lt;' => '<',
513
+ '&gt;' => '>',
514
+ '&amp;' => '&',
515
+ '&quot;' => '"',
516
+ '&nbsp;' => "\xa0",
517
+ '&cent;' => "\xa2",
518
+ '&pound;' => "\xa3",
519
+ '&yen;' => "\xa5",
520
+ '&copy;' => "\xa9",
521
+ '&reg;' => "\xae",
522
+ '&euro;' => "\x80",
523
+ }
524
+ character_entities.each {|ce, c|
525
+ pdf.add_page()
526
+ page = pdf.get_page
527
+ pdf.write_html(ce, true, 0, true, 0)
528
+ pdf_text = pdf.get_html_text(page)
529
+ assert_equal '[' + ce + ']:' + c, '[' + ce + ']:' + pdf_text
530
+ }
531
+ end
532
+
533
+ test "write_html Character Entities test pre mode" do
534
+ pdf = MYPDF.new
535
+ pdf.set_print_header(false)
536
+
537
+ character_entities = {
538
+ '&lt;' => '<',
539
+ '&gt;' => '>',
540
+ '&amp;' => '&',
541
+ '&quot;' => '"',
542
+ '&nbsp;' => "\xa0",
543
+ '&cent;' => "\xa2",
544
+ '&pound;' => "\xa3",
545
+ '&yen;' => "\xa5",
546
+ '&copy;' => "\xa9",
547
+ '&reg;' => "\xae",
548
+ '&euro;' => "\x80",
549
+ }
550
+ character_entities.each {|ce, c|
551
+ pdf.add_page()
552
+ page = pdf.get_page
553
+ pdf.write_html('<pre>' + ce + '</pre>', true, 0, true, 0)
554
+ pdf_text = pdf.get_html_text(page)
555
+ assert_equal '[' + ce + ']:' + c, '[' + ce + ']:' + pdf_text
556
+ }
557
+ end
558
+
559
+ test "unhtmlentities test" do
560
+ pdf = RBPDF.new
561
+ character_entities = {
562
+ '&lt;' => '<',
563
+ '&gt;' => '>',
564
+ '&amp;' => '&',
565
+ '&quot;' => '"',
566
+ '&nbsp;' => "\xc2\xa0",
567
+ '&cent;' => "\xc2\xa2",
568
+ '&pound;' => "\xc2\xa3",
569
+ '&yen;' => "\xc2\xa5",
570
+ '&copy;' => "\xc2\xa9",
571
+ '&reg;' => "\xc2\xae",
572
+ '&euro;' => "\xe2\x82\xac",
573
+ }
574
+ character_entities.each {|ce, c|
575
+ text = pdf.unhtmlentities(ce)
576
+ text.force_encoding('ASCII-8BIT') if text.respond_to?(:force_encoding)
577
+ assert_equal '[' + ce + ']:' + c, '[' + ce + ']:' + text
578
+ }
579
+ end
580
+ end