Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/rbpdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14769,6 +14769,14 @@ def openHTMLTagHandler(dom, key, cell)
else
align = 'B'
end

# store original margin values
l_margin = @l_margin
r_margin = @r_margin

SetLeftMargin(@l_margin + @c_margin)
SetRightMargin(@r_margin + @c_margin)

prevy = @y
xpos = @x
# eliminate marker spaces
Expand Down Expand Up @@ -14806,8 +14814,7 @@ def openHTMLTagHandler(dom, key, cell)
iw = 0
if tag['width']
# Calculate available width for percentage-based widths
available_width = @rtl ? (@x - @l_margin) : (@w - @r_margin - @x)
available_width -= 2 * @c_margin if available_width > 0
available_width = @w - @l_margin - @r_margin
iw = getHTMLUnitToUnits(tag['width'], available_width, 'px', false)
end
ih = 0
Expand All @@ -14817,13 +14824,6 @@ def openHTMLTagHandler(dom, key, cell)
ih = getHTMLUnitToUnits(tag['height'], available_height, 'px', false)
end

# store original margin values
l_margin = @l_margin
r_margin = @r_margin

SetLeftMargin(@l_margin + @c_margin)
SetRightMargin(@r_margin + @c_margin)

result_img =
proc_image_file(tag['attribute']['src']) do |img_file|
Image(img_file, xpos, @y, iw, ih, '', imglink, align, false, 300, '', false, false, border, false, false, true)
Expand Down
100 changes: 99 additions & 1 deletion test/rbpdf_html_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: ASCII-8BIT
# frozen_string_literal: true
#
# Copyright (c) 2011-2017 NAITOH Jun
# Copyright (c) 2011-2025 NAITOH Jun
# Released under the MIT license
# http://www.opensource.org/licenses/MIT

Expand Down Expand Up @@ -636,6 +636,104 @@ def get_html_text(page)
assert_equal ' 1.text A2.text B', pdf_text # A space is placed before the img tag.
end

IMG_PATH = File.join(File.dirname(__FILE__), '..', 'logo_example.png')

htmls = {
'rtl:false, width=100%' => {html: "<img src='#{IMG_PATH}' style='width: 100%'>", percentage: 100, rtl: false},
'rtl:true, width=100%' => {html: "<img src='#{IMG_PATH}' style='width: 100%'>", percentage: 100, rtl: true},
'rtl:false, width=10%' => {html: "<img src='#{IMG_PATH}' style='width: 10%'>", percentage: 10, rtl: false},
'rtl:true, width=10%' => {html: "<img src='#{IMG_PATH}' style='width: 10%'>", percentage: 10, rtl: true},
'rtl:false, width=100% string' => {html: "test<img src='#{IMG_PATH}' style='width: 100%'>", percentage: 100, rtl: false},
'rtl:true, width=100% string' => {html: "test<img src='#{IMG_PATH}' style='width: 100%'>", percentage: 100, rtl: true},
}

data(htmls)
test "write_html_cell image style percentage width" do |data|
pdf = RBPDF.new
pdf.set_rtl(data[:rtl], true)
pdf.add_page()

margins = pdf.get_margins
l_margin = margins['left']
r_margin = margins['right']
c_margin = margins['cell']
w = pdf.get_page_width
x0 = pdf.get_x
assert_equal l_margin, x0

pdf.write_html(data[:html])
no = pdf.get_num_pages
assert_equal 1, no

width = w - l_margin - r_margin - c_margin * 2
img_rb_x = pdf.get_image_rbx

if data[:rtl] == false
# NOTE: rtl=false
# +- x0 -+ c_margin +- width -+
# +----------------------------------- @w ----------------------------------------------+
# +- @l_margin -+- @c_margin -+- available_width -+- @c_margin -+- @r_margin -+
# |
# @img_rb_x
# +- @l_margin -+- @c_margin -+- available_width * 0.8 -+
# |
# @img_rb_x
assert_in_delta(x0 + c_margin + width * (data[:percentage] / 100.0), img_rb_x, 0.1)
else
# NOTE: rtl=true
# +- x0 -+ c_margin +- width -+
# +----------------------------------- @w ----------------------------------------------+
# +- @l_margin -+- @c_margin -+- available_width -+- @c_margin -+- @r_margin -+
# |
# @img_rb_x
# +- available_width * 0.8 -+- @c_margin -+- @r_margin -+
# |
# @img_rb_x
assert_in_delta(w - r_margin - c_margin - width * (data[:percentage] / 100.0), img_rb_x, 0.1)
end
end

htmls = {
'height=100%' => {html: "<img src='#{IMG_PATH}' style='height: 100%'>", percentage: 100, pno: 1},
'height=10%' => {html: "<img src='#{IMG_PATH}' style='height: 10%'>", percentage: 10, pno: 1},
'height=100% string' => {html: "test<br><img src='#{IMG_PATH}' style='height: 100%'>", percentage: 100, pno: 3},
}

data(htmls)
test "write_html_cell image style percentage height" do |data|
pdf = RBPDF.new
pdf.add_page('L')

margins = pdf.get_margins
t_margin = margins['top']
b_margin = margins['bottom']

w = pdf.get_page_width
h = pdf.get_page_height

y0 = pdf.get_y
assert_equal t_margin, y0

pdf.write_html(data[:html], false)
no = pdf.get_num_pages
assert_equal data[:pno], no

height = h - t_margin - b_margin
img_rb_y = pdf.get_image_rby

# NOTE:
# + +
# | @t_margin
# | +------------------+---------------+ y0
# | | | |
# @h available_hight height available_hight * 0.8
# | | | +- @img_rb_y
# | +------------------+- @img_rb_y
# | @b_margin
# + +
assert_in_delta(y0 + height * (data[:percentage] / 100.0), img_rb_y, 0.1)
end

test "write_html Character Entities test" do
pdf = MYPDF.new
pdf.set_print_header(false)
Expand Down
64 changes: 0 additions & 64 deletions test/rbpdf_htmlcell_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,68 +77,4 @@ class RbpdfTest < Test::Unit::TestCase
no = pdf.get_num_pages
assert_equal 1, no
end

test "write_html_cell with image percentage width" do
# Test that images with percentage-based width styles are actually embedded
pdf = RBPDF.new
pdf.add_page()

# Test image path - using the logo_example.png from the repo
img_path = File.join(File.dirname(__FILE__), '..', 'logo_example.png')

# Create PDF with image using percentage width
html_with_percentage = "<p>Image with percentage width:</p><img src='#{img_path}' style='width: 30%'>"
pdf.write_html_cell(0, 0, '', '', html_with_percentage)

# Check if the image was embedded
images = pdf.instance_variable_get('@images')
assert_equal 1, images.length, "Expected 1 image to be embedded in cache"

# More importantly, check that the PDF actually contains image data
# by verifying the PDF size is much larger than an empty PDF
output = pdf.output('', 'S')

# Create a reference PDF without any image
pdf_no_img = RBPDF.new
pdf_no_img.add_page()
pdf_no_img.write_html_cell(0, 0, '', '', '<p>Image with percentage width:</p>')
output_no_img = pdf_no_img.output('', 'S')

# PDF with image should be significantly larger (image data embedded)
assert output.length > output_no_img.length + 10000,
"Expected PDF with image (#{output.length} bytes) to be much larger than PDF without image (#{output_no_img.length} bytes). Image may not have been embedded."
end

test "write_html_cell with image percentage width generates output" do
pdf = RBPDF.new
pdf.add_page()

img_path = File.join(File.dirname(__FILE__), '..', 'logo_example.png')

# Test both plain and percentage width styles
html = <<-HTML
<h3>Test: Image rendering with percentage width</h3>
<p>Image without style (should work):</p>
<img src='#{img_path}'>
<p>Image with width=50 attribute (should work):</p>
<img src='#{img_path}' width='50'>
<p>Image with style width percentage (should work):</p>
<img src='#{img_path}' style='width: 30%'>
<p>Image with style width pixels (should work):</p>
<img src='#{img_path}' style='width: 50px'>
HTML

pdf.write_html_cell(0, 0, '', '', html)

# Generate PDF output to verify it doesn't crash
output = pdf.output('', 'S')
assert_not_nil output
assert output.length > 0

# Optionally write to file for manual inspection
if ENV['OUTPUT']
File.write('test_image_percentage_width.pdf', output)
puts "Generated test_image_percentage_width.pdf for manual inspection"
end
end
end
61 changes: 29 additions & 32 deletions test/rbpdf_image_rmagick_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: ASCII-8BIT
# frozen_string_literal: true
# Copyright (c) 2011-2023 NAITOH Jun
# Copyright (c) 2011-2025 NAITOH Jun
# Released under the MIT license
# http://www.opensource.org/licenses/MIT

Expand Down Expand Up @@ -177,40 +177,37 @@ class RbpdfTest < Test::Unit::TestCase
assert_equal "#{data[:file]} #{data[:info]}", "#{data[:file]} #{info}"
end

images = {
'png_test_alpha.png' => {file: 'png_test_alpha.png', x: 188, y: 34.8},
'png_test_msk_alpha.png' => {file: 'png_test_msk_alpha.png', x: 188, y: 34.8},
'png_test_non_alpha.png' => {file: 'png_test_non_alpha.png', x: 188, y: 34.8},
'webp_test_alpha.webp' => {file: 'webp_test_alpha.webp', x: 188, y: 34.8},
'logo_rbpdf_8bit.png' => {file: 'logo_rbpdf_8bit.png', x: 84.7, y: 31.4},
'logo_rbpdf_8bit.webp' => {file: 'logo_rbpdf_8bit.webp', x: 84.7, y: 31.4},
'logo_rbpdf_8bit.gif' => {file: 'logo_rbpdf_8bit.gif', x: 84.7, y: 31.4},
'logo_rbpdf_8bit_alpha.gif' => {file: 'logo_rbpdf_8bit_alpha.gif', x: 84.7, y: 31.4},
'logo_rbpdf_8bit.jpg' => {file: 'logo_rbpdf_8bit.jpg', x: 84.7, y: 31.4},
'logo_rbpdf_mono_gray.jpg' => {file: 'logo_rbpdf_mono_gray.jpg', x: 84.7, y: 31.4},
'logo_rbpdf_mono_gray.png' => {file: 'logo_rbpdf_mono_gray.png', x: 84.7, y: 31.4},
'logo_rbpdf_mono_rgb.jpg' => {file: 'logo_rbpdf_mono_rgb.jpg', x: 84.7, y: 31.4},
'logo_rbpdf_mono_rgb.png' => {file: 'logo_rbpdf_mono_rgb.png', x: 84.7, y: 31.4},
}

data(images)
test "HTML Image test" do
return unless Object.const_defined?(:Magick) or Object.const_defined?(:MiniMagick)

images = {
'png_test_alpha.png' => 40.11,
'png_test_msk_alpha.png' => 40.11,
'png_test_non_alpha.png' => 40.11,
'webp_test_alpha.webp' => 40.11,
'logo_rbpdf_8bit.png' => 36.58,
'logo_rbpdf_8bit.webp' => 36.58,
'logo_rbpdf_8bit.gif' => 36.58,
'logo_rbpdf_8bit_alpha.gif' => 36.58,
'logo_rbpdf_8bit.jpg' => 36.58,
'logo_rbpdf_mono_gray.jpg' => 36.58,
'logo_rbpdf_mono_gray.png' => 36.58,
'logo_rbpdf_mono_rgb.jpg' => 36.58,
'logo_rbpdf_mono_rgb.png' => 36.58,
'ng.png' => 9.42
}

pdf = RBPDF.new
images.each {|image, h|
pdf.add_page
img_file = File.join(File.dirname(__FILE__), image)
htmlcontent = '<img src="'+ img_file + '"/>'

x_org = pdf.get_x
y_org = pdf.get_y
pdf.write_html(htmlcontent, true, 0, true, 0)
x = pdf.get_x
y = pdf.get_y

assert_equal '[' + image + ']:' + x_org.to_s, '[' + image + ']:' + x.to_s
assert_equal '[' + image + ']:' + (y_org + h).round(2).to_s, '[' + image + ']:' + y.round(2).to_s
}
pdf.add_page
c_margin = pdf.get_margins['cell']

x_org = pdf.get_x
y_org = pdf.get_y
pdf.write_html("<img src='#{File.join(File.dirname(__FILE__), data[:file])}'/>")
x = pdf.get_image_rbx
y = pdf.get_image_rby

assert_in_delta data[:x], x - x_org - c_margin, 0.1
assert_in_delta data[:y], y - y_org, 0.1
end
end
Loading