rbpdf 1.18.4 → 1.18.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Rbpdf
2
- VERSION = "1.18.4"
2
+ VERSION = "1.18.5"
3
3
  end
@@ -227,7 +227,7 @@ class RbpdfCssTest < ActiveSupport::TestCase
227
227
  assert_equal dom[4]['attribute'], {'border'=>'2px #ff0000 solid', 'style'=>';border:2px #ff0000 solid;'}
228
228
  assert_equal dom[4]['style']['border'], '2px #ff0000 solid'
229
229
  assert_equal dom[4]['attribute']['border'], '2px #ff0000 solid'
230
- assert_equal dom[4]['thead'], '<style>table {;border:2px #ff0000 solid;}</style><table><tr><th>abc<marker style="font-size:0"/></th></tr></tablehead>'
230
+ assert_equal dom[4]['thead'], '<style>table {;border:2px #ff0000 solid;}</style><table tablehead="1"><tr><th>abc<marker style="font-size:0"/></th></tr></table>'
231
231
  end
232
232
 
233
233
  test "CSS Dom line-height test normal" do
@@ -323,7 +323,67 @@ class RbpdfCssTest < ActiveSupport::TestCase
323
323
  assert_equal dom[3]['value'], 'span'
324
324
  assert_equal dom[3]['style']['color'], '#006600'
325
325
  assert_equal dom[3]['style']['font-style'], 'italic'
326
+ end
327
+
328
+ test "CSS Dom height width test" do
329
+ pdf = MYPDF.new
330
+
331
+ html = '<style> p.first { height: 60%; }
332
+ p.second { width: 70%; }</style>
333
+ <p class="first">ABC</p><p class="second">DEF</p>'
334
+ dom = pdf.getHtmlDomArray(html)
335
+ assert_equal dom.length, 7
336
+
337
+ # <p class="first">
338
+ assert_equal dom[1]['elkey'], 0
339
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
340
+ assert_equal dom[1]['tag'], true
341
+ assert_equal dom[1]['opening'], true
342
+ assert_equal dom[1]['value'], 'p'
343
+ assert_not_nil dom[1]['style']
344
+ assert_equal dom[1]['style']['height'], '60%'
345
+ assert_equal dom[1]['height'], '60%'
346
+
347
+ # ABC
348
+ assert_equal dom[2]['elkey'], 1
349
+ assert_equal dom[2]['parent'], 1
350
+ assert_equal dom[2]['tag'], false
351
+ assert_equal dom[2]['value'], 'ABC'
352
+
353
+ # <p class="second">
354
+ assert_equal dom[4]['elkey'], 3
355
+ assert_equal dom[4]['parent'], 0 # parent -> parent tag key
356
+ assert_equal dom[4]['tag'], true
357
+ assert_equal dom[4]['opening'], true
358
+ assert_equal dom[4]['value'], 'p'
359
+ assert_not_nil dom[4]['style']
360
+ assert_equal dom[4]['style']['width'], '70%'
361
+ assert_equal dom[4]['width'], '70%'
362
+ end
363
+
364
+ test "CSS Dom font-weight test" do
365
+ pdf = MYPDF.new
366
+
367
+ html = '<style> p.first { font-weight: bold; }</style>
368
+ <p class="first">ABC</p><p class="second">DEF</p>'
369
+ dom = pdf.getHtmlDomArray(html)
370
+ assert_equal dom.length, 7
371
+
372
+ # <p class="first">
373
+ assert_equal dom[1]['elkey'], 0
374
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
375
+ assert_equal dom[1]['tag'], true
376
+ assert_equal dom[1]['opening'], true
377
+ assert_equal dom[1]['value'], 'p'
378
+ assert_not_nil dom[1]['style']
379
+ assert_equal dom[1]['style']['font-weight'], 'bold'
380
+ assert_equal dom[1]['fontstyle'], 'B'
326
381
 
382
+ # ABC
383
+ assert_equal dom[2]['elkey'], 1
384
+ assert_equal dom[2]['parent'], 1
385
+ assert_equal dom[2]['tag'], false
386
+ assert_equal dom[2]['value'], 'ABC'
327
387
  end
328
388
 
329
389
  test "CSS Dom id test" do
@@ -358,4 +418,219 @@ class RbpdfCssTest < ActiveSupport::TestCase
358
418
  assert_equal dom[3]['value'], 'span'
359
419
  assert_equal dom[3]['style']['background-color'], '#FFFFAA'
360
420
  end
421
+
422
+ test "CSS Dom text-decoration test" do
423
+ pdf = MYPDF.new
424
+
425
+ html = '<style> p.first { text-decoration: none;}
426
+ p.second {text-decoration: underline;}
427
+ p.third {text-decoration: overline;}
428
+ p.fourth {text-decoration: line-through;}
429
+ p.fifth {text-decoration: underline overline line-through;}</style>
430
+ <p class="first">ABC</p><p class="second">DEF</p><p class="third">GHI</p><p class="fourth">JKL</p><p class="fifth">MNO</p>'
431
+ dom = pdf.getHtmlDomArray(html)
432
+ assert_equal dom.length, 16
433
+
434
+ # <p class="first">
435
+ assert_equal dom[1]['elkey'], 0
436
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
437
+ assert_equal dom[1]['tag'], true
438
+ assert_equal dom[1]['opening'], true
439
+ assert_equal dom[1]['value'], 'p'
440
+ assert_not_nil dom[1]['style']
441
+ assert_equal dom[1]['style']['text-decoration'], 'none'
442
+ assert_equal dom[1]['fontstyle'], ''
443
+
444
+ # ABC
445
+ assert_equal dom[2]['elkey'], 1
446
+ assert_equal dom[2]['parent'], 1
447
+ assert_equal dom[2]['tag'], false
448
+ assert_equal dom[2]['value'], 'ABC'
449
+
450
+ # <p class="second">
451
+ assert_equal dom[4]['elkey'], 3
452
+ assert_equal dom[4]['parent'], 0 # parent -> parent tag key
453
+ assert_equal dom[4]['tag'], true
454
+ assert_equal dom[4]['opening'], true
455
+ assert_equal dom[4]['value'], 'p'
456
+ assert_not_nil dom[1]['style']
457
+ assert_equal dom[4]['style']['text-decoration'], 'underline'
458
+ assert_equal dom[4]['fontstyle'], 'U'
459
+
460
+ # <p class="third">
461
+ assert_equal dom[7]['elkey'], 6
462
+ assert_equal dom[7]['parent'], 0 # parent -> parent tag key
463
+ assert_equal dom[7]['tag'], true
464
+ assert_equal dom[7]['opening'], true
465
+ assert_equal dom[7]['value'], 'p'
466
+ assert_not_nil dom[7]['style']
467
+ assert_equal dom[7]['style']['text-decoration'], 'overline'
468
+ assert_equal dom[7]['fontstyle'], 'O'
469
+
470
+ # <p class="fourth">
471
+ assert_equal dom[10]['elkey'], 9
472
+ assert_equal dom[10]['parent'], 0 # parent -> parent tag key
473
+ assert_equal dom[10]['tag'], true
474
+ assert_equal dom[10]['opening'], true
475
+ assert_equal dom[10]['value'], 'p'
476
+ assert_not_nil dom[10]['style']
477
+ assert_equal dom[10]['style']['text-decoration'], 'line-through'
478
+ assert_equal dom[10]['fontstyle'], 'D'
479
+
480
+ # <p class="fifth">
481
+ assert_equal dom[13]['elkey'], 12
482
+ assert_equal dom[13]['parent'], 0 # parent -> parent tag key
483
+ assert_equal dom[13]['tag'], true
484
+ assert_equal dom[13]['opening'], true
485
+ assert_equal dom[13]['value'], 'p'
486
+ assert_not_nil dom[13]['style']
487
+ assert_equal dom[13]['style']['text-decoration'], 'underline overline line-through'
488
+ assert_equal dom[13]['fontstyle'], 'UOD'
489
+ end
490
+
491
+ test "CSS Dom text-indent test" do
492
+ pdf = MYPDF.new
493
+
494
+ html = '<style> p.first { text-indent: 10px; }
495
+ p.second { text-indent: 5em; }
496
+ p.third { text-indent: 5ex; }
497
+ p.fourth { text-indent: 50%; }</style>
498
+ <p class="first">ABC</p><p class="second">DEF</p><p class="third">GHI</p><p class="fourth">JKL</p>'
499
+ dom = pdf.getHtmlDomArray(html)
500
+ assert_equal dom.length, 13
501
+
502
+ # <p class="first">
503
+ assert_equal dom[1]['elkey'], 0
504
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
505
+ assert_equal dom[1]['tag'], true
506
+ assert_equal dom[1]['opening'], true
507
+ assert_equal dom[1]['value'], 'p'
508
+ assert_not_nil dom[1]['style']
509
+ assert_equal dom[1]['style']['text-indent'], '10px'
510
+ assert_in_delta dom[1]['text-indent'], 3.53, 0.01
511
+
512
+ # ABC
513
+ assert_equal dom[2]['elkey'], 1
514
+ assert_equal dom[2]['parent'], 1
515
+ assert_equal dom[2]['tag'], false
516
+ assert_equal dom[2]['value'], 'ABC'
517
+
518
+ # <p class="second">
519
+ assert_equal dom[4]['elkey'], 3
520
+ assert_equal dom[4]['parent'], 0 # parent -> parent tag key
521
+ assert_equal dom[4]['tag'], true
522
+ assert_equal dom[4]['opening'], true
523
+ assert_equal dom[4]['value'], 'p'
524
+ assert_not_nil dom[4]['style']
525
+ assert_equal dom[4]['style']['text-indent'], '5em'
526
+ assert_equal dom[4]['text-indent'], 5.0
527
+
528
+ # <p class="third">
529
+ assert_equal dom[7]['elkey'], 6
530
+ assert_equal dom[7]['parent'], 0 # parent -> parent tag key
531
+ assert_equal dom[7]['tag'], true
532
+ assert_equal dom[7]['opening'], true
533
+ assert_equal dom[7]['value'], 'p'
534
+ assert_not_nil dom[7]['style']
535
+ assert_equal dom[7]['style']['text-indent'], '5ex'
536
+ assert_equal dom[7]['text-indent'], 2.5
537
+
538
+ # <p class="fourth">
539
+ assert_equal dom[10]['elkey'], 9
540
+ assert_equal dom[10]['parent'], 0 # parent -> parent tag key
541
+ assert_equal dom[10]['tag'], true
542
+ assert_equal dom[10]['opening'], true
543
+ assert_equal dom[10]['value'], 'p'
544
+ assert_not_nil dom[10]['style']
545
+ assert_equal dom[10]['style']['text-indent'], '50%'
546
+ assert_equal dom[10]['text-indent'], 0.5
547
+ end
548
+
549
+ test "CSS Dom list-style-type test" do
550
+ pdf = MYPDF.new
551
+
552
+ html = '<style> p.first { list-style-type: none; }
553
+ p.second { list-style-type: disc; }
554
+ p.third { list-style-type: circle; }
555
+ p.fourth { list-style-type: square; }</style>
556
+ <p class="first">ABC</p><p class="second">DEF</p><p class="third">GHI</p><p class="fourth">JKL</p>'
557
+ dom = pdf.getHtmlDomArray(html)
558
+ assert_equal dom.length, 13
559
+
560
+ # <p class="first">
561
+ assert_equal dom[1]['elkey'], 0
562
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
563
+ assert_equal dom[1]['tag'], true
564
+ assert_equal dom[1]['opening'], true
565
+ assert_equal dom[1]['value'], 'p'
566
+ assert_not_nil dom[1]['style']
567
+ assert_equal dom[1]['style']['list-style-type'], 'none'
568
+ assert_equal dom[1]['listtype'], 'none'
569
+
570
+ # ABC
571
+ assert_equal dom[2]['elkey'], 1
572
+ assert_equal dom[2]['parent'], 1
573
+ assert_equal dom[2]['tag'], false
574
+ assert_equal dom[2]['value'], 'ABC'
575
+
576
+ # <p class="second">
577
+ assert_equal dom[4]['elkey'], 3
578
+ assert_equal dom[4]['parent'], 0 # parent -> parent tag key
579
+ assert_equal dom[4]['tag'], true
580
+ assert_equal dom[4]['opening'], true
581
+ assert_equal dom[4]['value'], 'p'
582
+ assert_not_nil dom[4]['style']
583
+ assert_equal dom[4]['style']['list-style-type'], 'disc'
584
+ assert_equal dom[4]['listtype'], 'disc'
585
+
586
+ # <p class="third">
587
+ assert_equal dom[7]['elkey'], 6
588
+ assert_equal dom[7]['parent'], 0 # parent -> parent tag key
589
+ assert_equal dom[7]['tag'], true
590
+ assert_equal dom[7]['opening'], true
591
+ assert_equal dom[7]['value'], 'p'
592
+ assert_not_nil dom[7]['style']
593
+ assert_equal dom[7]['style']['list-style-type'], 'circle'
594
+ assert_equal dom[7]['listtype'], 'circle'
595
+ end
596
+
597
+ test "CSS Dom page-break test" do
598
+ pdf = MYPDF.new
599
+
600
+ html = '<style> p.first { page-break-before: left; page-break-after: always; }
601
+ p.second { page-break-inside:avoid; }</style>
602
+ <p class="first">ABC</p><p class="second">DEF</p>'
603
+ dom = pdf.getHtmlDomArray(html)
604
+ assert_equal dom.length, 7
605
+
606
+ # <p class="first">
607
+ assert_equal dom[1]['elkey'], 0
608
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
609
+ assert_equal dom[1]['tag'], true
610
+ assert_equal dom[1]['opening'], true
611
+ assert_equal dom[1]['value'], 'p'
612
+ assert_not_nil dom[1]['style']
613
+ assert_equal dom[1]['style']['page-break-before'], 'left'
614
+ assert_equal dom[1]['style']['page-break-after'], 'always'
615
+ assert_not_nil dom[1]['attribute']
616
+ assert_equal dom[1]['attribute']['pagebreak'], 'left'
617
+ assert_equal dom[1]['attribute']['pagebreakafter'], 'true'
618
+
619
+ # ABC
620
+ assert_equal dom[2]['elkey'], 1
621
+ assert_equal dom[2]['parent'], 1
622
+ assert_equal dom[2]['tag'], false
623
+ assert_equal dom[2]['value'], 'ABC'
624
+
625
+ # <p class="second">
626
+ assert_equal dom[4]['elkey'], 3
627
+ assert_equal dom[4]['parent'], 0 # parent -> parent tag key
628
+ assert_equal dom[4]['tag'], true
629
+ assert_equal dom[4]['opening'], true
630
+ assert_equal dom[4]['value'], 'p'
631
+ assert_not_nil dom[1]['style']
632
+ assert_equal dom[4]['style']['page-break-inside'], 'avoid'
633
+ assert_not_nil dom[4]['attribute']
634
+ assert_equal dom[4]['attribute']['nobr'], 'true'
635
+ end
361
636
  end
@@ -47,23 +47,24 @@ class RbpdfTest < ActiveSupport::TestCase
47
47
 
48
48
  # Error Tag (doble colse tag)
49
49
  dom = pdf.getHtmlDomArray('</ul></div>')
50
- assert_equal dom.length, 3
51
50
 
52
51
  assert_equal 0, dom[0]['parent'] # Root
53
52
  assert_equal false, dom[0]['tag']
54
53
  assert_equal({}, dom[0]['attribute'])
55
54
 
56
- assert_equal dom[1]['parent'], 0 # parent -> Root key
57
- assert_equal dom[1]['elkey'], 0
58
- assert_equal dom[1]['tag'], true
59
- assert_equal dom[1]['opening'], false
60
- assert_equal dom[1]['value'], 'ul'
61
-
62
- assert_equal dom[2]['parent'], 0 # parent -> Root key
63
- assert_equal dom[2]['elkey'], 1
64
- assert_equal dom[2]['tag'], true
65
- assert_equal dom[2]['opening'], false
66
- assert_equal dom[2]['value'], 'div'
55
+ if dom.length == 3 # for Rails 3.x/4.0/4.1 (no use Rails 4.2 later)
56
+ assert_equal dom[1]['parent'], 0 # parent -> Root key
57
+ assert_equal dom[1]['elkey'], 0
58
+ assert_equal dom[1]['tag'], true
59
+ assert_equal dom[1]['opening'], false
60
+ assert_equal dom[1]['value'], 'ul'
61
+
62
+ assert_equal dom[2]['parent'], 0 # parent -> Root key
63
+ assert_equal dom[2]['elkey'], 1
64
+ assert_equal dom[2]['tag'], true
65
+ assert_equal dom[2]['opening'], false
66
+ assert_equal dom[2]['value'], 'div'
67
+ end
67
68
 
68
69
  # Attribute
69
70
  dom = pdf.getHtmlDomArray('<p style="text-align:justify">abc</p>')
@@ -78,8 +79,8 @@ class RbpdfTest < ActiveSupport::TestCase
78
79
  assert_equal dom[1]['tag'], true
79
80
  assert_equal dom[1]['opening'], true
80
81
  assert_equal dom[1]['value'], 'p'
81
- assert_equal dom[1]['attribute'], {'style'=>'text-align: justify;'}
82
- assert_equal dom[1]['attribute']['style'], 'text-align: justify;'
82
+ assert_not_nil dom[1]['attribute']
83
+ assert_equal dom[1]['attribute']['style'].gsub(' ', ''), 'text-align:justify;'
83
84
  assert_equal dom[1]['align'], 'J'
84
85
 
85
86
  # Table border
@@ -265,7 +266,7 @@ class RbpdfTest < ActiveSupport::TestCase
265
266
  assert_equal temprtl, false
266
267
  end
267
268
 
268
- test "Dom HTMLTagHandler img test" do
269
+ test "Dom HTMLTagHandler img y position with height attribute test" do
269
270
  pdf = MYPDF.new
270
271
  pdf.add_page
271
272
 
@@ -280,6 +281,21 @@ class RbpdfTest < ActiveSupport::TestCase
280
281
  assert_equal pdf.get_image_rby - (12 / pdf.get_scale_factor) , y2
281
282
  end
282
283
 
284
+ test "Dom HTMLTagHandler img y position without height attribute test" do
285
+ pdf = MYPDF.new
286
+ pdf.add_page
287
+
288
+ # Image Error HTML
289
+ htmlcontent = '<img src="' + Rails.root.to_s + '/public/ng.png" alt="test alt attribute" border="0"/>'
290
+ dom1 = pdf.getHtmlDomArray(htmlcontent)
291
+ y1 = pdf.get_y
292
+
293
+ dom2 = pdf.openHTMLTagHandler(dom1, 1, false)
294
+ y2 = pdf.get_y
295
+ assert_equal dom1, dom2
296
+ assert_equal y1, y2
297
+ end
298
+
283
299
  test "getHtmlDomArray encoding test" do
284
300
  return unless 'test'.respond_to?(:force_encoding)
285
301
 
@@ -5,6 +5,9 @@ class RbpdfTest < ActiveSupport::TestCase
5
5
  def addHTMLVertSpace(hbz, hb, cell, firstorlast)
6
6
  super
7
7
  end
8
+ def sanitize_html(html)
9
+ super
10
+ end
8
11
  end
9
12
 
10
13
  test "html func addHTMLVertSpace empty lines test" do
@@ -133,4 +136,27 @@ class RbpdfTest < ActiveSupport::TestCase
133
136
  assert_equal x4, x3
134
137
  assert_equal y4, y3 + 10 + 2
135
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>
144
+ <tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr>
145
+ <tr><td>' + 'ABC' + '</td></tr></table>'
146
+ html = pdf.sanitize_html(html).gsub(/[\r\n]/,'')
147
+
148
+ 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>}
149
+ end
150
+
151
+ test "html func sanitize test 2" do
152
+ pdf = MYPDF.new
153
+ pdf.add_page()
154
+
155
+ 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>'
156
+ html = '<table cellpadding="1"><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead>
157
+ <tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr>
158
+ <tr><td>' + htmlcontent + '</td></tr></table>'
159
+ html = pdf.sanitize_html(html).gsub(/[\r\n]/,'')
160
+ 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>}
161
+ end
136
162
  end
@@ -11,7 +11,6 @@ class RbpdfTest < ActiveSupport::TestCase
11
11
  pdf = RBPDF.new
12
12
  pdf.add_page()
13
13
 
14
-
15
14
  htmlcontent = '<h1>HTML Example</h1>'
16
15
  pdf.write_html(htmlcontent, true, 0, true, 0)
17
16
 
@@ -25,7 +24,7 @@ class RbpdfTest < ActiveSupport::TestCase
25
24
  assert_equal pno, 3
26
25
  end
27
26
 
28
- test "write_html Table test" do
27
+ test "write_html Table test 1" do
29
28
  pdf = RBPDF.new
30
29
  pdf.add_page()
31
30
 
@@ -41,6 +40,63 @@ class RbpdfTest < ActiveSupport::TestCase
41
40
  assert_equal pno, 3
42
41
  end
43
42
 
43
+ test "write_html Table test 2" do
44
+ pdf = MYPDF.new
45
+ pdf.add_page()
46
+
47
+ 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>'
48
+
49
+ tablehtml = '<table border="1"><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr>
50
+ <tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr>
51
+ <tr><td>' + htmlcontent + '</td></tr></table>'
52
+ pdf.write_html(tablehtml, true, 0, true, 0)
53
+
54
+ pno = pdf.get_page
55
+ assert_equal pno, 3
56
+
57
+ content = []
58
+ contents = pdf.getPageBuffer(1)
59
+ contents.each_line {|line| content.push line.chomp }
60
+ count = 0
61
+ count_text = 0
62
+ pos1 = -1
63
+ pos2 = -2
64
+ content.each do |line|
65
+ count_text += 1 if line =~ /TJ ET Q$/
66
+ if line =~ /ABCD/
67
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
68
+ pos1 = $1
69
+ assert_not_nil pos1
70
+ end
71
+ if line =~ /abcd/
72
+ count += 1
73
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
74
+ pos2 = $1
75
+ assert_not_nil pos2
76
+ end
77
+ end
78
+ assert_equal count_text, 13
79
+ assert_equal count, 1
80
+ assert_equal pos1, pos2
81
+
82
+ content = []
83
+ contents = pdf.getPageBuffer(2)
84
+ contents.each_line {|line| content.push line.chomp }
85
+ count_head = 0
86
+ count = 0
87
+ count_text = 0
88
+ content.each do |line|
89
+ count_text += 1 if line =~ /TJ ET Q$/
90
+ if line =~ /\([6-9]\)/
91
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
92
+ pos2 = $1
93
+ assert_not_nil pos2
94
+ assert_equal pos1, pos2
95
+ end
96
+ end
97
+ assert_equal count_text, 7
98
+ end
99
+
44
100
  test "write_html Table thead tag test 1" do
45
101
  pdf = MYPDF.new
46
102
  pdf.add_page()
@@ -71,7 +127,7 @@ class RbpdfTest < ActiveSupport::TestCase
71
127
 
72
128
  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>'
73
129
 
74
- tablehtml = '<table border="1" cellpadding="1" cellspacing="1"><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead>
130
+ tablehtml = '<table><thead><tr><td>ABCD</td><td>EFGH</td><td>IJKL</td></tr></thead>
75
131
  <tr><td>abcd</td><td>efgh</td><td>ijkl</td></tr>
76
132
  <tr><td>' + htmlcontent + '</td></tr></table>'
77
133
 
@@ -79,43 +135,402 @@ class RbpdfTest < ActiveSupport::TestCase
79
135
  page = pdf.get_page
80
136
  assert_equal 3, page
81
137
 
138
+ # Page 1
82
139
  content = []
83
140
  contents = pdf.getPageBuffer(1)
84
141
  contents.each_line {|line| content.push line.chomp }
85
- count_head = 0
86
- count = 0
142
+ count_text = count_head = count = 0
143
+ pos1 = -1
144
+ pos2 = -2
87
145
  content.each do |line|
88
- count_head += 1 if line =~ /ABCD/
89
- count += 1 if line =~ /abcd/
146
+ count_text += 1 if line =~ /TJ ET Q$/
147
+ if line =~ /ABCD/
148
+ count_head += 1
149
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
150
+ pos1 = $1
151
+ assert_not_nil pos1
152
+ end
153
+ if line =~ /abcd/
154
+ count += 1
155
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
156
+ pos2 = $1
157
+ assert_not_nil pos2
158
+ end
90
159
  end
160
+ assert_equal count_text, 13
91
161
  assert_equal count_head, 1
92
162
  assert_equal count, 1
163
+ assert_equal pos1, pos2
93
164
 
165
+ # Page 2
94
166
  content = []
95
167
  contents = pdf.getPageBuffer(2)
96
168
  contents.each_line {|line| content.push line.chomp }
97
- count_head = 0
98
- count = 0
169
+ count_text = count_head = count = 0
99
170
  content.each do |line|
100
- count_head += 1 if line =~ /ABCD/
171
+ count_text += 1 if line =~ /TJ ET Q$/
172
+ if line =~ /ABCD/
173
+ count_head += 1
174
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
175
+ pos2 = $1
176
+ assert_not_nil pos2
177
+ assert_equal pos1, pos2
178
+ end
101
179
  count += 1 if line =~ /abcd/
180
+ if line =~ /\([6-9]\)/
181
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
182
+ pos2 = $1
183
+ assert_not_nil pos2
184
+ assert_equal pos1, pos2
185
+ end
102
186
  end
187
+ assert_equal count_text, 10
103
188
  assert_equal count_head, 1
104
189
  assert_equal count, 0
105
190
 
191
+ # Page 3
106
192
  content = []
107
193
  contents = pdf.getPageBuffer(3)
108
194
  contents.each_line {|line| content.push line.chomp }
109
- count_head = 0
110
- count = 0
195
+ count_text = count_head = count = 0
111
196
  content.each do |line|
112
- count_head += 1 if line =~ /ABCD/
197
+ count_text += 1 if line =~ /TJ ET Q$/
198
+ if line =~ /ABCD/
199
+ count_head += 1
200
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
201
+ pos2 = $1
202
+ assert_not_nil pos2
203
+ assert_equal pos1, pos2
204
+ end
113
205
  count += 1 if line =~ /abcd/
206
+ if line =~ /\(11\)/
207
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
208
+ pos2 = $1
209
+ assert_not_nil pos2
210
+ assert_equal pos1, pos2
211
+ end
114
212
  end
213
+ assert_equal count_text, 5
115
214
  assert_equal count_head, 1
116
215
  assert_equal count, 0
117
216
  end
118
217
 
218
+ test "write_html_cell Table thead tag test" do
219
+ pdf = MYPDF.new
220
+ pdf.add_page()
221
+
222
+ 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>
223
+ <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>
224
+ <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>
225
+ <br><br><br><br><br><br><br><br>'
226
+
227
+ tablehtml ='<table><thead><tr>
228
+ <th style="text-align: left">Left align</th>
229
+ <th style="text-align: right">Right align</th>
230
+ <th style="text-align: center">Center align</th>
231
+ </tr> </thead><tbody> <tr>
232
+ <td style="text-align: left">left' + htmlcontent + '</td>
233
+ <td style="text-align: right">right</td>
234
+ <td style="text-align: center">center</td>
235
+ </tr> </tbody></table>'
236
+
237
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
238
+
239
+ page = pdf.get_page
240
+ assert_equal 1, page
241
+
242
+ # Page 1
243
+ content = []
244
+ contents = pdf.getPageBuffer(1)
245
+ contents.each_line {|line| content.push line.chomp }
246
+ count_text = count_head = count = 0
247
+ pos1 = -1
248
+ pos2 = -2
249
+ content.each do |line|
250
+ count_text += 1 if line =~ /TJ ET Q$/
251
+ if line =~ /Left align/
252
+ count_head += 1
253
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
254
+ pos1 = $1
255
+ assert_not_nil pos1
256
+ end
257
+ if line =~ /left/
258
+ count += 1
259
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
260
+ pos2 = $1
261
+ assert_not_nil pos2
262
+ end
263
+ end
264
+ assert_equal count_text, 13
265
+ assert_equal count_head, 1
266
+ assert_equal count, 1
267
+ assert_equal pos1, pos2
268
+
269
+ # Page 2
270
+ content = []
271
+ contents = pdf.getPageBuffer(2)
272
+ contents.each_line {|line| content.push line.chomp }
273
+ count_text = count_head = count = 0
274
+ content.each do |line|
275
+ count_text += 1 if line =~ /TJ ET Q$/
276
+ if line =~ /Left align/
277
+ count_head += 1
278
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
279
+ pos2 = $1
280
+ assert_not_nil pos2
281
+ assert_equal pos1, pos2
282
+ end
283
+ if line =~ /\(6\)/
284
+ count += 1
285
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
286
+ pos2 = $1
287
+ assert_not_nil pos2
288
+ assert_equal pos1, pos2
289
+ end
290
+ end
291
+ assert_equal count_text, 10
292
+ assert_equal count_head, 1
293
+ assert_equal count, 1
294
+
295
+ # Page 3
296
+ content = []
297
+ contents = pdf.getPageBuffer(3)
298
+ contents.each_line {|line| content.push line.chomp }
299
+ count_text = count_head = count = 0
300
+ content.each do |line|
301
+ count_text += 1 if line =~ /TJ ET Q$/
302
+ if line =~ /Left align/
303
+ count_head += 1
304
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
305
+ pos2 = $1
306
+ assert_not_nil pos2
307
+ assert_equal pos1, pos2
308
+ end
309
+ if line =~ /\(11\)/
310
+ count += 1
311
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
312
+ pos2 = $1
313
+ assert_not_nil pos2
314
+ assert_equal pos1, pos2
315
+ end
316
+ end
317
+ assert_equal count_text, 5
318
+ assert_equal count_head, 1
319
+ assert_equal count, 1
320
+ end
321
+
322
+ test "write_html_cell Table thead tag cellpadding x position test" do
323
+ pdf = MYPDF.new
324
+ pdf.add_page()
325
+
326
+ 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>
327
+ <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>
328
+ <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>
329
+ <br><br><br><br><br><br><br><br>'
330
+
331
+ tablehtml ='<table cellpadding="10"><thead><tr>
332
+ <th style="text-align: left">Left align</th>
333
+ <th style="text-align: right">Center align</th>
334
+ <th style="text-align: left">Right align</th>
335
+ </tr> </thead><tbody> <tr>
336
+ <td style="text-align: left">left</td>
337
+ <td style="text-align: right">center</td>
338
+ <td style="text-align: left">right' + htmlcontent + '</td>
339
+ </tr> </tbody></table>'
340
+
341
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
342
+
343
+ page = pdf.get_page
344
+ assert_equal 1, page
345
+
346
+ # Page 1
347
+ content = []
348
+ contents = pdf.getPageBuffer(1)
349
+ contents.each_line {|line| content.push line.chomp }
350
+ count_text = count_head = count = 0
351
+ pos1 = -1
352
+ pos2 = -2
353
+ content.each do |line|
354
+ count_text += 1 if line =~ /TJ ET Q$/
355
+ if line =~ /Right align/
356
+ count_head += 1
357
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
358
+ pos1 = $1
359
+ assert_not_nil pos1
360
+ end
361
+ if line =~ /right/
362
+ count += 1
363
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
364
+ pos2 = $1
365
+ assert_not_nil pos2
366
+ end
367
+ end
368
+ assert_equal count_text, 13
369
+ assert_equal count_head, 1
370
+ assert_equal count, 1
371
+ assert_equal pos1, pos2
372
+
373
+ # Page 2
374
+ content = []
375
+ contents = pdf.getPageBuffer(2)
376
+ contents.each_line {|line| content.push line.chomp }
377
+ count_text = count_head = count = 0
378
+ content.each do |line|
379
+ count_text += 1 if line =~ /TJ ET Q$/
380
+ if line =~ /Right align/
381
+ count_head += 1
382
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
383
+ pos2 = $1
384
+ assert_not_nil pos2
385
+ assert_equal pos1, pos2
386
+ end
387
+ if line =~ /\(6\)/
388
+ count += 1
389
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
390
+ pos2 = $1
391
+ assert_not_nil pos2
392
+ assert_equal pos1, pos2
393
+ end
394
+ end
395
+ assert_equal count_text, 10
396
+ assert_equal count_head, 1
397
+ assert_equal count, 1
398
+ end
399
+
400
+ test "write_html_cell Table thead tag cellpadding y position test 1" do
401
+ pdf = MYPDF.new
402
+ pdf.add_page()
403
+
404
+ table_start='<table cellpadding="10"><thead><tr>
405
+ <th style="text-align: left">Left align</th><th style="text-align: center">Center align</th><th style="text-align: right">Right align</th>
406
+ </tr></thead><tbody>'
407
+ 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>'
408
+ table_end='</tbody></table>'
409
+ tablehtml= table_start + table_col * 30 + table_end
410
+
411
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
412
+
413
+ # Page 1
414
+ content = []
415
+ contents = pdf.getPageBuffer(1)
416
+ contents.each_line {|line| content.push line.chomp }
417
+ count_text = count_head = count = 0
418
+ pos1 = pos2 = -1
419
+ content.each do |line|
420
+ count_text += 1 if line =~ /TJ ET Q$/
421
+ if line =~ /Left align/
422
+ count_head += 1
423
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
424
+ pos1 = $2
425
+ assert_not_nil pos1
426
+ end
427
+ if line =~ /AAA/
428
+ count += 1
429
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
430
+ pos2 = $2 if pos2 == -1
431
+ assert_not_nil pos2
432
+ end
433
+ end
434
+
435
+ assert_equal count_text, 65
436
+ assert_equal count_head, 1
437
+ assert_equal count, 20
438
+ base_pos = pos1.to_i - pos2.to_i
439
+
440
+ # Page 2
441
+ content = []
442
+ contents = pdf.getPageBuffer(2)
443
+ contents.each_line {|line| content.push line.chomp }
444
+ count_text = count_head = count = 0
445
+ pos1 = pos2 = -1
446
+ content.each do |line|
447
+ count_text += 1 if line =~ /TJ ET Q$/
448
+ if line =~ /Left align/
449
+ count_head += 1
450
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
451
+ pos1 = $2
452
+ assert_not_nil pos1
453
+ end
454
+ if line =~ /AAA/
455
+ count += 1
456
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
457
+ pos2 = $2 if pos2 == -1
458
+ assert_not_nil pos2
459
+ end
460
+ end
461
+ assert_equal count_text, 34
462
+ assert_equal count_head, 1
463
+ assert_equal count, 10
464
+ assert_equal base_pos, pos1.to_i - pos2.to_i
465
+ end
466
+
467
+ test "write_html_cell Table thead tag cellpadding y position test 2" do
468
+ pdf = MYPDF.new
469
+ pdf.add_page()
470
+
471
+ table_start='abc<br><table cellpadding="10"><thead><tr>
472
+ <th style="text-align: left">Left align</th><th style="text-align: center">Center align</th><th style="text-align: right">Right align</th>
473
+ </tr></thead><tbody>'
474
+ 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>'
475
+ table_end='</tbody></table>'
476
+ tablehtml= table_start + table_col * 30 + table_end
477
+
478
+ pdf.write_html_cell(0, 0, '', '',tablehtml)
479
+
480
+ # Page 1
481
+ content = []
482
+ contents = pdf.getPageBuffer(1)
483
+ contents.each_line {|line| content.push line.chomp }
484
+ count_text = count_head = count = 0
485
+ pos1 = pos2 = -1
486
+ content.each do |line|
487
+ count_text += 1 if line =~ /TJ ET Q$/
488
+ if line =~ /Left align/
489
+ count_head += 1
490
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
491
+ pos1 = $2
492
+ assert_not_nil pos1
493
+ end
494
+ if line =~ /AAA/
495
+ count += 1
496
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
497
+ pos2 = $2 if pos2 == -1
498
+ assert_not_nil pos2
499
+ end
500
+ end
501
+
502
+ assert_equal count_text, 66
503
+ assert_equal count_head, 1
504
+ assert_equal count, 20
505
+ base_pos = pos1.to_i - pos2.to_i
506
+
507
+ # Page 2
508
+ content = []
509
+ contents = pdf.getPageBuffer(2)
510
+ contents.each_line {|line| content.push line.chomp }
511
+ count_text = count_head = count = 0
512
+ pos1 = pos2 = -1
513
+ content.each do |line|
514
+ count_text += 1 if line =~ /TJ ET Q$/
515
+ if line =~ /Left align/
516
+ count_head += 1
517
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
518
+ pos1 = $2
519
+ assert_not_nil pos1
520
+ end
521
+ if line =~ /AAA/
522
+ count += 1
523
+ line =~ /BT ([0-9.]+) ([0-9.]+) Td/
524
+ pos2 = $2 if pos2 == -1
525
+ assert_not_nil pos2
526
+ end
527
+ end
528
+ assert_equal count_text, 34
529
+ assert_equal count_head, 1
530
+ assert_equal count, 10
531
+ assert_equal base_pos, pos1.to_i - pos2.to_i
532
+ end
533
+
119
534
  test "write_html ASCII text test" do
120
535
  pdf = MYPDF.new
121
536
  pdf.add_page()