From 8015dca1f32532e9c8a7e12982f5db222e5133fd Mon Sep 17 00:00:00 2001 From: blocknotes Date: Sat, 2 Jul 2022 08:19:59 +0200 Subject: [PATCH 1/6] Rename elements tag_open_styles and tag_close_styles methods --- lib/prawn_html/document_renderer.rb | 4 ++-- lib/prawn_html/tag.rb | 8 ++++---- spec/units/prawn_html/document_renderer_spec.rb | 4 ++-- spec/units/prawn_html/tag_spec.rb | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 32f5cb7..920f5d1 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -94,14 +94,14 @@ def render_if_needed(element) end def apply_tag_close_styles(element) - tag_styles = element.tag_close_styles + tag_styles = element.tag_closing @last_margin = tag_styles[:margin_bottom].to_f pdf.advance_cursor(last_margin + tag_styles[:padding_bottom].to_f) pdf.start_new_page if tag_styles[:break_after] end def apply_tag_open_styles(element) - tag_styles = element.tag_open_styles + tag_styles = element.tag_opening move_down = (tag_styles[:margin_top].to_f - last_margin) + tag_styles[:padding_top].to_f pdf.advance_cursor(move_down) if move_down > 0 pdf.start_new_page if tag_styles[:break_before] diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb index 1f0505f..f28f271 100644 --- a/lib/prawn_html/tag.rb +++ b/lib/prawn_html/tag.rb @@ -53,17 +53,17 @@ def process_styles(element_styles: nil) attrs.merge_text_styles!(extra_styles, options: options) if respond_to?(:extra_styles) end - # Styles to apply on tag closing + # Tag closing callback that applies tag's specific styles # # @return [Hash] hash of styles to apply - def tag_close_styles + def tag_closing styles.slice(*Attributes::STYLES_APPLY[:tag_close]) end - # Styles to apply on tag opening + # Tag opening callback that applies tag's specific styles # # @return [Hash] hash of styles to apply - def tag_open_styles + def tag_opening styles.slice(*Attributes::STYLES_APPLY[:tag_open]) end diff --git a/spec/units/prawn_html/document_renderer_spec.rb b/spec/units/prawn_html/document_renderer_spec.rb index e9b72d6..e9accd4 100644 --- a/spec/units/prawn_html/document_renderer_spec.rb +++ b/spec/units/prawn_html/document_renderer_spec.rb @@ -18,12 +18,12 @@ before do allow(context).to receive(:remove_last) - allow(element).to receive(:tag_close_styles).and_call_original + allow(element).to receive(:tag_closing).and_call_original end it 'handles tag closing', :aggregate_failures do on_tag_close - expect(element).to have_received(:tag_close_styles) + expect(element).to have_received(:tag_closing) expect(context).to have_received(:remove_last) end end diff --git a/spec/units/prawn_html/tag_spec.rb b/spec/units/prawn_html/tag_spec.rb index e6e4d89..87755fd 100644 --- a/spec/units/prawn_html/tag_spec.rb +++ b/spec/units/prawn_html/tag_spec.rb @@ -94,14 +94,14 @@ def tag_styles it { is_expected.to eq(color: '0088ff') } end - describe '#tag_close_styles' do - subject(:tag_close_styles) { tag.tag_close_styles } + describe '#tag_closing' do + subject(:tag_closing) { tag.tag_closing } it { is_expected.to eq({}) } end - describe '#tag_open_styles' do - subject(:tag_open_styles) { tag.tag_open_styles } + describe '#tag_opening' do + subject(:tag_opening) { tag.tag_opening } it { is_expected.to eq({}) } end From 26401c62a5580169fa8cf5fa1e568771568cfe0f Mon Sep 17 00:00:00 2001 From: blocknotes Date: Sat, 9 Jul 2022 09:37:20 +0200 Subject: [PATCH 2/6] Add prawn-table dependency --- lib/prawn_html/pdf_wrapper.rb | 3 ++- prawn-html.gemspec | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/prawn_html/pdf_wrapper.rb b/lib/prawn_html/pdf_wrapper.rb index 7a816ab..72a02d4 100644 --- a/lib/prawn_html/pdf_wrapper.rb +++ b/lib/prawn_html/pdf_wrapper.rb @@ -1,12 +1,13 @@ # frozen_string_literal: true require 'forwardable' +require 'prawn/table' module PrawnHtml class PdfWrapper extend Forwardable - def_delegators :@pdf, :start_new_page + def_delegators :@pdf, :start_new_page, :table # Wrapper for Prawn PDF Document # diff --git a/prawn-html.gemspec b/prawn-html.gemspec index 954d174..f4deede 100644 --- a/prawn-html.gemspec +++ b/prawn-html.gemspec @@ -26,4 +26,5 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'oga', '~> 3.3' spec.add_runtime_dependency 'prawn', '~> 2.4' + spec.add_runtime_dependency 'prawn-table', '~> 0.2.2' end From 011a8089c089e5bc53e5357583eb21c720c9a3e5 Mon Sep 17 00:00:00 2001 From: blocknotes Date: Sat, 9 Jul 2022 09:43:40 +0200 Subject: [PATCH 3/6] Add context argument to tag opening and closing callbacks --- .rubocop.yml | 3 +-- lib/prawn_html/document_renderer.rb | 4 ++-- lib/prawn_html/tag.rb | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 64b4698..9fede3e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,8 +16,7 @@ AllCops: NewCops: enable Lint/UnusedMethodArgument: - Exclude: - - lib/prawn_html/utils.rb + Enabled: false Naming/FileName: Exclude: diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 920f5d1..ea855c8 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -94,14 +94,14 @@ def render_if_needed(element) end def apply_tag_close_styles(element) - tag_styles = element.tag_closing + tag_styles = element.tag_closing(context: context) @last_margin = tag_styles[:margin_bottom].to_f pdf.advance_cursor(last_margin + tag_styles[:padding_bottom].to_f) pdf.start_new_page if tag_styles[:break_after] end def apply_tag_open_styles(element) - tag_styles = element.tag_opening + tag_styles = element.tag_opening(context: context) move_down = (tag_styles[:margin_top].to_f - last_margin) + tag_styles[:padding_top].to_f pdf.advance_cursor(move_down) if move_down > 0 pdf.start_new_page if tag_styles[:break_before] diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb index f28f271..bc613a0 100644 --- a/lib/prawn_html/tag.rb +++ b/lib/prawn_html/tag.rb @@ -56,14 +56,14 @@ def process_styles(element_styles: nil) # Tag closing callback that applies tag's specific styles # # @return [Hash] hash of styles to apply - def tag_closing + def tag_closing(context: nil) styles.slice(*Attributes::STYLES_APPLY[:tag_close]) end # Tag opening callback that applies tag's specific styles # # @return [Hash] hash of styles to apply - def tag_opening + def tag_opening(context: nil) styles.slice(*Attributes::STYLES_APPLY[:tag_open]) end From 235bda9f8ebce3c9af9656da29f715fac87d5ce9 Mon Sep 17 00:00:00 2001 From: blocknotes Date: Sat, 9 Jul 2022 09:44:41 +0200 Subject: [PATCH 4/6] New tags: table, tr, td --- lib/prawn_html/tag.rb | 2 +- lib/prawn_html/tags/table.rb | 38 ++++++++++++++++++++++++++++++++++++ lib/prawn_html/tags/td.rb | 19 ++++++++++++++++++ lib/prawn_html/tags/tr.rb | 19 ++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 lib/prawn_html/tags/table.rb create mode 100644 lib/prawn_html/tags/td.rb create mode 100644 lib/prawn_html/tags/tr.rb diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb index bc613a0..533046f 100644 --- a/lib/prawn_html/tag.rb +++ b/lib/prawn_html/tag.rb @@ -9,7 +9,7 @@ class Tag 'StrikeThrough' => Callbacks::StrikeThrough }.freeze - TAG_CLASSES = %w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup U Ul].freeze + TAG_CLASSES = %w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup Table Td Tr U Ul].freeze def_delegators :@attrs, :styles, :update_styles diff --git a/lib/prawn_html/tags/table.rb b/lib/prawn_html/tags/table.rb new file mode 100644 index 0000000..b744a8a --- /dev/null +++ b/lib/prawn_html/tags/table.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module PrawnHtml + module Tags + class Table < Tag + ELEMENTS = [:table].freeze + + attr_reader :table_data + + def block? + true + end + + def new_cell + @col_index += 1 + @table_data[@row_index] << '' + end + + def new_row + @row_index += 1 + @col_index = -1 + @table_data << [] + end + + def update_content(content) + @table_data[@row_index][@col_index] = content + end + + def tag_opening(context: nil) + super.tap do + context.current_table = self + @row_index = -1 + @table_data = [] + end + end + end + end +end diff --git a/lib/prawn_html/tags/td.rb b/lib/prawn_html/tags/td.rb new file mode 100644 index 0000000..29d29c9 --- /dev/null +++ b/lib/prawn_html/tags/td.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module PrawnHtml + module Tags + class Td < Tag + ELEMENTS = [:td].freeze + + def block? + true + end + + def tag_opening(context: nil) + super.tap do + context.current_table&.new_cell + end + end + end + end +end diff --git a/lib/prawn_html/tags/tr.rb b/lib/prawn_html/tags/tr.rb new file mode 100644 index 0000000..d065760 --- /dev/null +++ b/lib/prawn_html/tags/tr.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module PrawnHtml + module Tags + class Tr < Tag + ELEMENTS = [:tr].freeze + + def block? + true + end + + def tag_opening(context: nil) + super.tap do + context.current_table&.new_row + end + end + end + end +end From 3620a048d821497bcff682c1246ce93359a590c9 Mon Sep 17 00:00:00 2001 From: blocknotes Date: Sat, 9 Jul 2022 09:45:25 +0200 Subject: [PATCH 5/6] Handle tables contents --- examples/tables.html | 26 ++++++++++++++++++++++++++ examples/tables.pdf | Bin 0 -> 4068 bytes lib/prawn_html/context.rb | 3 ++- lib/prawn_html/document_renderer.rb | 13 ++++++++++--- 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 examples/tables.html create mode 100644 examples/tables.pdf diff --git a/examples/tables.html b/examples/tables.html new file mode 100644 index 0000000..45223f2 --- /dev/null +++ b/examples/tables.html @@ -0,0 +1,26 @@ + + + + Tables + + +
Tables
+ + + + + + + + + + + + + + + + +
A 1A 2A 3
B 1B 2B 3
C 1C 2C 3
+ + diff --git a/examples/tables.pdf b/examples/tables.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d42766fb6f88419c0ee7eac729d756382a86213d GIT binary patch literal 4068 zcmcInU2mI86nx)bu`hWnEG#S_S(a)yZqv_P8|gk+d7$<>r83Y0Y197s-m}2jC2?b~ zdsQnD$-tRCJDkDH#r2EDGtc4V;um~~M;Wa?ekPL%b?3Fr+N!3Lo!sph3mIdP&zSJw z7{O6aiF;jF+q;cCdeL-BWVuzW{)j#O@kPQKS)1*vTOi7AWkWe!uq38H{g3VR1>JST zomY2dOFiPg$+rz%hpO&oR2Ym3-^%i~{X{u*uC8cW*jGWm5;98(7Y?IQk~o}mpQc;l zE_^@|N6>Uf){`)p(wNbS@PscmB8>R-hNiE{WlBJ+@5Fr}zvUabd^w}@6=6_{zpMFs zWDlG;jssc{5jZ09VZ8y_lNDLtK&#u13`^C~lt-S!JTWdu#Hu_A9p10r?=@mhsOU}< z>sEC+B9;V7-2hsrQ4~8O2!dW+g?ON}5;yRC#=VDvKGpzNMac@1aEAk%t%yi~q8S22 z{JmnAbd_d%clzR2f(8ch$2}EzV6U8H64CRPe z5*UMrffGgk?+otS8yv^ykNt_F83IK9wi0@Pz5dZU>cH+^C@In4$HlZXxDnG>V;IsC zV&Gm-yOF^WvSffBhQZRDc4~}U9vC!q#%!X&j|*yLaNPMHI@ITmul!-k5ji1cZ*W+> z`~F0O!y~)1+lgXrXI+knC4n(GXHLS{?+niE4Q@o#^DsR4EH{Fp8G9jONnnILQ;AW- z!<|6n3@Opz$HlZXxDnG>!%j`zxgJW*U@0du7&#HKR6{Rl7&K%_G5!WWF6hYM{}2^K zhWGy=lBncxm@rfxsU;+gK9IQYZ9>5gj5gXvHoT58N=$srcT2bh0Y3kmb!IxaY<`EU&$K^Gcp>nz{Px=ZA%{HKJL l-DOp^RE24{@mJ-pqAIp`xw&bxy6yJHWA2lSi^~rS@*gb{YDWM7 literal 0 HcmV?d00001 diff --git a/lib/prawn_html/context.rb b/lib/prawn_html/context.rb index c5250da..6ea340b 100644 --- a/lib/prawn_html/context.rb +++ b/lib/prawn_html/context.rb @@ -7,11 +7,12 @@ class Context < Array }.freeze attr_reader :previous_tag - attr_accessor :last_text_node + attr_accessor :last_text_node, :current_table # Init the Context def initialize(*_args) super + @current_table = nil @last_text_node = false @merged_styles = nil @previous_tag = nil diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index ea855c8..2f65590 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -66,7 +66,13 @@ def on_text_node(content) def render return if buffer.empty? - output_content(buffer.dup, context.block_styles) + content = prepare_content(buffer.dup, context.block_styles) + if context.current_table + td_content = content.dig(:buffer, 0, :text) + context.current_table.update_content(td_content) + else + pdf.puts(content[:buffer], content[:options], left_indent: content[:left_indent], bounding_box: content[:bounds]) + end buffer.clear @last_margin = 0 end @@ -95,6 +101,7 @@ def render_if_needed(element) def apply_tag_close_styles(element) tag_styles = element.tag_closing(context: context) + pdf.table(element.table_data) if element.is_a?(Tags::Table) @last_margin = tag_styles[:margin_bottom].to_f pdf.advance_cursor(last_margin + tag_styles[:padding_bottom].to_f) pdf.start_new_page if tag_styles[:break_after] @@ -118,12 +125,12 @@ def prepare_text(content) @last_text = text end - def output_content(buffer, block_styles) + def prepare_content(buffer, block_styles) apply_callbacks(buffer) left_indent = block_styles[:margin_left].to_f + block_styles[:padding_left].to_f options = block_styles.slice(:align, :indent_paragraphs, :leading, :mode, :padding_left) options[:leading] = adjust_leading(buffer, options[:leading]) - pdf.puts(buffer, options, bounding_box: bounds(buffer, options, block_styles), left_indent: left_indent) + { buffer: buffer, options: options, left_indent: left_indent, bounds: bounds(buffer, options, block_styles) } end def apply_callbacks(buffer) From 2993e429e2eaaec59da1aba52001de8721baf0a1 Mon Sep 17 00:00:00 2001 From: Julian Rubisch Date: Mon, 5 Sep 2022 09:31:46 +0200 Subject: [PATCH 6/6] chore: Fix closing tag --- lib/prawn_html/tags/table.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/prawn_html/tags/table.rb b/lib/prawn_html/tags/table.rb index b744a8a..7c37f71 100644 --- a/lib/prawn_html/tags/table.rb +++ b/lib/prawn_html/tags/table.rb @@ -33,6 +33,12 @@ def tag_opening(context: nil) @table_data = [] end end + + def tag_closing(context: nil) + super.tap do + context.current_table = nil + end + end end end end