Skip to content

Commit

Permalink
simplifies encoding and enables table growing
Browse files Browse the repository at this point in the history
  • Loading branch information
threez committed Apr 21, 2024
1 parent 903daf6 commit 29c5f4c
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 138 deletions.
8 changes: 4 additions & 4 deletions examples/accented.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "../src/hpdf"

k = "Können wir mit Umlauten umgehen? Ja von Kopf bis Fuß.".encode("ISO8859-2")

pdf = Hpdf::Doc.build do
page do
text Hpdf::Base14::Helvetica, 20, encoding: "ISO8859-2" do
text_out :center, height - 50, k
use_encoding Hpdf::Encodings::ISO8859_2 do
text Hpdf::Base14::Helvetica, 20 do
text_out :center, height - 50, "Können wir mit Umlauten umgehen? Ja von Kopf bis Fuß."
end
end
end
end
Expand Down
Binary file modified montage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pdfs/examples-accented.pdf
Binary file not shown.
Binary file modified pdfs/spec-doc-attributes.pdf
Binary file not shown.
Binary file modified pdfs/spec-doc-password.pdf
Binary file not shown.
Binary file modified pdfs/spec-invoice.pdf
Binary file not shown.
Binary file modified pdfs/spec-table.pdf
Binary file not shown.
184 changes: 94 additions & 90 deletions spec/hpdf/invoice_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ describe Hpdf::Letter do
# debug_draw_boxes

# Header
draw_address company: "Evil Corp",
salutation: "Mr.",
name: "Robot",
street: "Street 1",
place: "New York",
country: "USA"
use_encoding Hpdf::Encodings::ISO8859_2 do
draw_address company: "Evil Cörp",
salutation: "Mr.",
name: "Robot",
street: "Street 1",
place: "New York",
country: "USA"

draw_remark_area first: "Good Corp | Awesomestr. 20 | 12345 Place"
draw_remark_area first: "Good Corp | Awesomestr. 20 | 12345 Place"

draw_infobox Hpdf::Base14::Helvetica, 12 do
row "Your contact:", "Max Mustermann"
row "Department:", "Customer Service"
row
row "Phone:", "09161 620-9800"
row "Fax:", "09161 8989-2000"
row "E-Mail:", "[email protected]"
row
row "Date:", "2022-02-01"
draw_infobox Hpdf::Base14::Helvetica, 12 do
row "Your contact:", "Max Müstermann"
row "Department:", "Customer Service"
row
row "Phone:", "09161 620-9800"
row "Fax:", "09161 8989-2000"
row "E-Mail:", "[email protected]"
row
row "Date:", "2022-02-01"
end
end

# Content
Expand All @@ -39,80 +41,82 @@ describe Hpdf::Letter do
"\n\nKind regards your service team"
end

table(content_rect.padding(top: 150), fixed_row_height: 22) do
row do
text_cell "Position",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
text_cell "Description",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
span: 4,
bg_gray: 0.9
text_cell "Quantity",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
text_cell "Price",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
text_cell "Total",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
end
row do
text_cell "1"
text_cell "iPhone 12",
span: 4
text_cell "2",
align: Hpdf::TextAlignment::Right
text_cell "899.00",
align: Hpdf::TextAlignment::Right
text_cell "1798.00",
align: Hpdf::TextAlignment::Right
end
row do
text_cell "2"
text_cell "MacBookPro 14\"",
span: 4
text_cell "1",
align: Hpdf::TextAlignment::Right
text_cell "2499.00",
align: Hpdf::TextAlignment::Right
text_cell "2499.00",
align: Hpdf::TextAlignment::Right
end
row do
text_cell "3"
text_cell "iPad",
span: 4
text_cell "1",
align: Hpdf::TextAlignment::Right
text_cell "499.00",
align: Hpdf::TextAlignment::Right
text_cell "499.00",
align: Hpdf::TextAlignment::Right
end
row { }
row do
text_cell "Total EUR",
span: 7,
align: Hpdf::TextAlignment::Right
text_cell "4796.00",
font: Hpdf::Base14::HelveticaBold,
align: Hpdf::TextAlignment::Right,
bg_gray: 0.9
end
row do
text_cell "incl. VAT 19%",
span: 7,
align: Hpdf::TextAlignment::Right
text_cell "765.75",
align: Hpdf::TextAlignment::Right,
bg_gray: 0.9
use_encoding Hpdf::Encodings::ISO8859_2 do
table(content_rect.padding(top: 150), fixed_row_height: 22) do
row do
text_cell "Position",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
text_cell "Description",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
span: 4,
bg_gray: 0.9
text_cell "Quantity",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
text_cell "Price",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
text_cell "Total",
align: Hpdf::TextAlignment::Center,
font: Hpdf::Base14::HelveticaBold,
bg_gray: 0.9
end
row do
text_cell "1"
text_cell "iPhone 12",
span: 4
text_cell "2",
align: Hpdf::TextAlignment::Right
text_cell "899.00",
align: Hpdf::TextAlignment::Right
text_cell "1798.00",
align: Hpdf::TextAlignment::Right
end
row allow_grow: true do
text_cell "2"
text_cell "MacBookPro 14\"\nBehold an entirely new class of GPU.\nAnd the biggest breakthrough in silicön",
span: 4
text_cell "1",
align: Hpdf::TextAlignment::Right
text_cell "2499.00",
align: Hpdf::TextAlignment::Right
text_cell "2499.00",
align: Hpdf::TextAlignment::Right
end
row do
text_cell "3"
text_cell "iPad",
span: 4
text_cell "1",
align: Hpdf::TextAlignment::Right
text_cell "499.00",
align: Hpdf::TextAlignment::Right
text_cell "499.00",
align: Hpdf::TextAlignment::Right
end
row { }
row do
text_cell "Total EUR",
span: 7,
align: Hpdf::TextAlignment::Right
text_cell "4796.00",
font: Hpdf::Base14::HelveticaBold,
align: Hpdf::TextAlignment::Right,
bg_gray: 0.9
end
row do
text_cell "incl. VAT 19%",
span: 7,
align: Hpdf::TextAlignment::Right
text_cell "765.75",
align: Hpdf::TextAlignment::Right,
bg_gray: 0.9
end
end
end

Expand Down
8 changes: 6 additions & 2 deletions src/hpdf/doc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,12 @@ module Hpdf
end

# gets the handle of a corresponding font object by specified name and encoding.
def font(name : String, encoding : String? = nil)
Font.new(LibHaru.get_font(@doc, name, encoding), self)
def font(name : String, encoding enc : String? = nil)
if enc
Font.new(LibHaru.get_font(@doc, name, enc), self)
else
Font.new(LibHaru.get_font(@doc, name, nil), self)
end
end

# loads a type1 font from an external file and register it to a document object.
Expand Down
Loading

0 comments on commit 29c5f4c

Please sign in to comment.