class Hpdf::Doc

Included Modules

Defined in:

hpdf/doc.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Hpdf::Helper

bool(val : Bool) : Int32 bool, nilable_str(v : Pointer(UInt8)) : String | Nil nilable_str, real(val : Number) : LibHaru::Real real, uint(val : Number) : LibHaru::UInt uint, uint16(val : Number) : UInt16 uint16

Constructor Detail

def self.new #

[View source]

Class Method Detail

def self.build(&) #

build enables DSL style access to building a doc


[View source]

Instance Method Detail

def add_custom_page(klass) #

custom pages are subclasses of Page that can have more methods for higher level page constructs.

class MyPage < Hpdf::Page
  # ...
end

pdf = Hpdf::Doc.new do |pdf|
page = pdf.add_custom_page(MyPage)
# ...

[View source]
def add_page : Page #

creates a new page and adds it after the last page of a document.


[View source]
def add_page_label(page_num : Number, style : PageNumStyle, *, first_page = 1, prefix : String | Nil = nil) #

adds a page labeling range for the document.

  • page_num the first page that applies this labeling range.
  • style the numbering style.
  • first_page the first page number in this range.
  • prefix the prefix for the page label.

[View source]
def author : String | Nil #

[View source]
def author=(v : String) #

[View source]
def build(&) #

build enables DSL style access to building a doc


[View source]
def compression_mode=(mode : CompressionMode) #

set the mode of compression.


[View source]
def create_outline(title : String, *, parent : Outline | Nil = nil, encoder : Encoder | Nil = nil) : Outline #

creates a new outline object.

  • title the caption of the outline object.
  • parent the handle of an outline object which comes to the parent of the created outline object. If this parameter is 'nil', The outline is created as a root outline.
  • encoder the handle of an encoding object applied to the title. If 'nil' is set, current_encoder is used.

[View source]
def creation_date : Time | Nil #

[View source]
def creation_date=(time : Time) #

[View source]
def creator : String | Nil #

[View source]
def creator=(v : String) #

[View source]
def current_page : Page | Nil #

the handle of current page object.


[View source]
def encoder : Encoder | Nil #

gets the handle of the current encoder of the document object. The current encoder is set by invoking #encoder= and it is used to processing a text when an application changes document attributes. The default value of it is 'nil'.


[View source]
def encoder=(encoding_name : String) #

sets the current encoder for the document.

  • encoding_name the name of an encoding. (See Encodings)

[View source]
def encoder=(enc : Encoder) #

see #encoder=.


[View source]
def finalize #

will free any related memory in case the document is not used any longer


[View source]
def find_encoder(name : String) : Encoder #

finds the handle of a corresponding encoder object by specified encoding name.

  • name specify a valid encoding name, see Encodings

[View source]
def font(name : String, encoding enc : String | Nil = nil) #

gets the handle of a corresponding font object by specified name and encoding.


[View source]
def insert_page(page : Page) : Page #

creates a new page and inserts it just before the specified #page.


[View source]
def keywords : String | Nil #

[View source]
def keywords=(v : String) #

[View source]
def load_jpeg_image_from_file(file_name : String) #

loads an external Jpeg image file.

  • file_name path to a jpeg image file.

[View source]
def load_png_image_from_file(file_name : String, *, lazy : Bool = false) : Image #

loads an external png image file.

  • file_name path to a PNG image file.
  • lazy if true does not load whole data immediately (only size and color properties is loaded). The main data is loaded just before the image object is written to PDF, and the loaded data is deleted immediately.

[View source]
def load_raw_image_from_file(file_name : String, width : Number, height : Number, color_space : ColorSpace) : Image #

loads an image which has "raw" image format. This function loads the data without any conversion. So it is usually faster than the other functions.

#load_raw_image_from_file can load 3 types of format described below.

ColorSpace::DeviceGray

The gray scale describes one pixel by one byte. And the size of the image data is same as width * height. The sequence of the data is as follows.

1
2
3
4
6
7
8
9
11
12
13
14

ColorSpace::DeviceRgb

The 24bit RGB color image describes one pixel by 3 byte (each one byte describes a value of either red, green or blue). And the size of the image is same as width * height * 3. The sequence of the data is as follows.

1
1
1
2
2
2
3
3
3
4
4
4
6
6
6
7
7
7
8
8
8
9
9
9
11
11
11
12
12
12
13
13
13
14
14
14

ColorSpace::DeviceCmyk

The 36bit CMYK color image describes one pixel by 4 byte (each one byte describes a value of either Cyan Magenta Yellow Black). And the size of the image is same as width * height * 4. The sequence of the data is as follows.

1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
6
6
6
6
7
7
7
7
8
8
8
8
9
9
9
9
11
11
11
11
12
12
12
12
13
13
13
13
14
14
14
14

[View source]
def load_raw_image_from_mem(buf, width : Number, height : Number, color_space : ColorSpace = ColorSpace::DeviceRgb, bits_per_component : UInt8 = 8) #

loads an image which has "raw" image format from buffer. This function loads the data without any conversion. So it is usually faster than the other functions.

The formats that #load_raw_image_from_mem can load is the same as #load_raw_image_from_file.


[View source]
def load_raw_image_from_mem(img : Raw::Image) #

loads an image which has "raw" image format from buffer. This function loads the data without any conversion. So it is usually faster than the other functions.


[View source]
def load_tt_font_from_collection_file(file_name : String, index : Number, embedding : Bool = true) : String #

loads a TrueType font from an TrueType collection file and register it to a document object. Returns the name of a font.

  • file_name path of a TrueType font collection file (.ttc).
  • index index of font that wants to be loaded.
  • embedding this parameter is set to true, the glyph data of the font is embedded, otherwise only the matrix data is included in PDF file.

[View source]
def load_tt_font_from_file(file_name : String, embedding : Bool = true) #

loads a TrueType font from an external file and register it to a document object. Returns the name of a font.

  • file_name path of a TrueType font file (.ttf).
  • embedding this parameter is set to true, the glyph data of the font is embedded, otherwise only the matrix data is included in PDF file.

[View source]
def load_type1_font_from_file(afm_file : String, data_file : String | Nil = nil) : String #

loads a type1 font from an external file and register it to a document object. Returns the name of a font.

  • afm_file path of an AFM file.
  • data_file path of a PFA/PFB file. If it is nil, the gryph data of font file is not embedded to a PDF file.

[View source]
def mod_date : Time | Nil #

[View source]
def mod_date=(time : Time) #

[View source]
def open_action=(dst : Destination) #

set the first page to appear when a document is opened.


[View source]
def page(klass = Page, &) #

page enables DSL style usage of the document. It calls #add_page and then executes the passed block in the context of the page.

  • klass can be changed to a subclass of Page in order to create methods for higher level page constructs.

Example with custom page class

class MyPage < Hpdf::Page
  # ...
end

Hpdf::Doc.build do |pdf|
  page(MyPage) do |page|
    # ...
  end
end

[View source]
def page_layout : PageLayout #

the current setting for page layout.


[View source]
def page_layout=(layout : PageLayout) #

sets how the page should be displayed. If this attribute is not set, the setting of the viewer application is used.


[View source]
def page_mode : PageMode #

the current setting for page mode.


[View source]
def page_mode=(mode : PageMode) #

sets how the document should be displayed.


[View source]
def pages_configuration=(page_per_pages : Number) #

In the default setting, a Doc object has one "Pages" object as root of pages. All "Page" objects are created as a kid of the "Pages" object. Since a "Pages" object can own only 8191 kids objects, the maximum number of pages are 8191 page.

http://libharu.sourceforge.net/image/figure7.png

Additionally, the state that there are a lot of "Page" object under one "Pages" object is not good, because it causes performance degradation of a viewer application.

An application can change the setting of a pages tree by invoking #pages_configuration= . If page_per_pages parameter is set to more than zero, a two-tier pages tree is created. A root "Pages" object can own 8191 "Pages" object, and each lower "Pages" object can own page_per_pages "Page" objects. As a result, the maximum number of pages becomes 8191 * page_per_pages page.

http://libharu.sourceforge.net/image/figure8.png

An application cannot invoke #pages_configuration= after a page is added to document.


[View source]
def save_to_file(path : String) #

saves the current document to a file.


[View source]
def set_password_and_permission(owner_password : String, *, user_password : String | Nil = nil, permission : Permission = Permission::EnableRead, encryption_mode : EncryptMode = EncryptMode::EncryptR3, encryption_key_len_bytes : Int32 = 16) #

sets the pasword for the document. If the password is set, contents in the document are encrypted.

  • owner_password the password for the owner of the document. The owner can change the permission of the document. nil, zero length string and the same value as user password are not allowed.
  • user_password the password for the user of the document. The user_password is allowed to be set to nil or zero length string.
  • permission the flags specifying which operations are permitted. This parameter is set by logical addition of the following values.

[View source]
def subject : String | Nil #

[View source]
def subject=(v : String) #

[View source]
def title : String | Nil #

[View source]
def title=(v : String) #

[View source]
def to_io : IO #

writes the document to an in memory IO object. This will require memory for the size of the document.


[View source]
def to_unsafe : LibHaru::Doc #

[View source]
def use_cns_encodings #

enables simplified Chinese encodings. After #use_cns_encodings is involed, an application can use the following simplified Chinese encodings.


[View source]
def use_cns_fonts #

enables Japanese fonts. After #use_cns_fonts is called, an application can use the following simplified Chinese fonts: ChineseSimplifiedFonts::All


[View source]
def use_cnt_encodings #

enables traditional Chinese encodings. After #use_cnt_encodings is involed, an application can use the following traditional Chinese encodings.


[View source]
def use_cnt_fonts #

enables Japanese fonts. After #use_jp_fonts is called, an application can use the following traditional Chinese fonts: ChineseTraditionalFonts::All


[View source]
def use_jp_encodings #

enables Japanese encodings. After #use_jp_encodings is involed, an application can use the following Japanese encodings.


[View source]
def use_jp_fonts #

enables Japanese fonts. After #use_jp_fonts is called, an application can use the following Japanese fonts: JapaneseFonts::All


[View source]
def use_kr_encodings #

enables Korean encodings. After #use_kr_encodings is involed, an application can use the following Korean encodings.


[View source]
def use_kr_fonts #

enables Japanese fonts. After #use_kr_fonts is called, an application can use the following Korean fonts: KoreanFonts::All


[View source]