class

Hpdf::Doc

Inherits Hpdf::Helper / Reference / Object

Constructors

Class methods

build

build enables DSL style access to building a doc

Source

Instance methods

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)
# ...
Source
add_page

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

Source
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.
Source
add_xmp_extension(xml : String)

Injects a raw XMP namespace extension block into the document XMP stream.

  • xml a well-formed XML string declaring additional XMP namespaces and schema descriptions, appended verbatim to the document's XMP packet.
Source
append_output_intents(name : String, profile : LibHaru::OutputIntent) : Nil

Appends a PDF/A /OutputIntents entry using an ICC color profile.

Required by PDF/A-1b and later for colour-space definition.

  • name the colour-space name that appears in the output intent (e.g. "sRGB").
  • profile a handle returned by load_icc_profile.
Source
attach_file(path : String, *, name : String = File.basename(path), description : String | Nil = nil, subtype : String = "text/xml", relationship : AFRelationship = AFRelationship::Alternative, size : UInt64 | Nil = nil, creation_date : Time | Nil = nil, modification_date : Time | Nil = nil) : self

Attaches a file from path on disk and configures its embedded-file metadata. The file must exist on disk until save_to_file or to_io is called. Returns self to allow chaining.

  • path path to the file on disk to embed.
  • name display name for the attachment (defaults to the file's basename).
  • description optional human-readable description of the attachment.
  • subtype MIME type of the attached file (default: "text/xml").
  • relationship how the attachment relates to the document (see AFRelationship).
  • size optional file size in bytes stored in the attachment metadata.
  • creation_date optional creation timestamp stored in the attachment metadata.
  • modification_date optional last-modification timestamp stored in the attachment metadata.
Source
author
Source
author=(v : String)
Source
build

build enables DSL style access to building a doc

Source
compression_mode=(mode : CompressionMode)

set the mode of compression.

Source
create_ext_g_state

creates a new extended graphics state object. Use it to set transparency and blend modes on a page via Page#ext_g_state=.

Source
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.
Source
create_shading(type : ShadingType, color_space : ColorSpace, x_min : Number, x_max : Number, y_min : Number, y_max : Number) : Shading

creates a new shading object.

  • type the type of shading (see ShadingType)
  • color_space the color space of the shading (see ColorSpace)
  • x_min, x_max, y_min, y_max the bounding box of the shading
Source
creation_date
Source
creation_date=(time : Time)
Source
creator
Source
creator=(v : String)
Source
current_page

the handle of current page object.

Source
encoder

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'.

Source
encoder=(encoding_name : String)

sets the current encoder for the document.

  • encoding_name the name of an encoding. (See Encodings)
Source
encoder=(enc : Encoder)

see encoder=.

Source
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
Source
font(name : String, encoding enc : String | Nil = nil)

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

Source
insert_page(page : Page) : Page

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

Source
keywords
Source
keywords=(v : String)
Source
load_icc_profile(path : String, num_component : Int32 = 3) : LibHaru::OutputIntent

Loads an ICC color profile from path and returns an output-intent handle.

  • path path to the ICC profile file on disk.
  • num_component number of color components: 1 (gray), 3 (RGB), or 4 (CMYK).

Pass the returned handle to append_output_intents.

Source
load_jpeg_image(source : String | Path) : Image

loads a JPEG image from a file path or a Bytes buffer.

  • source a file path (String or Path) or raw JPEG data (Bytes).
Source
load_jpeg_image(source : Bytes) : Image

loads a JPEG image from a file path or a Bytes buffer.

  • source a file path (String or Path) or raw JPEG data (Bytes).
Source
load_jpeg_image_from_file(file_name : String)

loads an external Jpeg image file.

  • file_name path to a jpeg image file.
Source
load_png_image(source : String | Path, lazy : Bool = false) : Image

loads a PNG image from a file path or a Bytes buffer.

  • source a file path (String or Path) or raw PNG data (Bytes).
  • lazy (file only) if true, defers loading the pixel data until the PDF is written, which reduces peak memory usage for large images.
Source
load_png_image(source : Bytes) : Image

loads a PNG image from a file path or a Bytes buffer.

  • source a file path (String or Path) or raw PNG data (Bytes).
  • lazy (file only) if true, defers loading the pixel data until the PDF is written, which reduces peak memory usage for large images.
Source
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.
Source
load_raw1_bit_image(source : Bytes, *, width : Number, height : Number, line_width : Number, black_is1 : Bool = true, top_is_first : Bool = true) : Image

loads a 1-bit bilevel image from a Bytes buffer. Each row occupies line_width bytes; individual pixels are packed MSB-first.

  • source raw image data.
  • width image width in pixels.
  • height image height in pixels.
  • line_width byte width of one scanline (must be ≥ ⌈width/8⌉).
  • black_is1 if true, a set bit is black; if false, a set bit is white.
  • top_is_first if true, the first byte row is the top of the image.
Source
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.

  • file_name path to a RAW image file.
  • width the width of the image file.
  • height the height of the image file.
  • color_space ColorSpace::DeviceGray or ColorSpace::DeviceRgb or ColorSpace::DeviceCmyk is allowed.

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
Source
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.

  • buf buffer with a raw memory image, has to implement #unsafe.
  • width the width of the image.
  • height the height of the image.
  • color_space ColorSpace::DeviceGray or ColorSpace::DeviceRgb or ColorSpace::DeviceCmyk is allowed.
  • bits_per_component The bit size of each color component. The valid value is either 1, 2, 4, 8.
Source
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.

Source
load_tt_font(source : String | Path, embedding : Bool = true) : String

loads a TrueType font from a file path or a Bytes buffer and returns the font name. The font name can be passed to Doc#font.

  • source a file path (String or Path) or raw font data (Bytes).
  • embedding if true the glyph data is embedded in the PDF.
Source
load_tt_font(source : Bytes, embedding : Bool = true) : String

loads a TrueType font from a file path or a Bytes buffer and returns the font name. The font name can be passed to Doc#font.

  • source a file path (String or Path) or raw font data (Bytes).
  • embedding if true the glyph data is embedded in the PDF.
Source
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.
Source
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.
Source
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.
Source
mod_date
Source
mod_date=(time : Time)
Source
new_doc

resets the document to a fresh state, clearing all pages and content.

Source
open_action=(dst : Destination)

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

Source
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
Source
page_by_index(index : Int) : Page

returns the page at the given 0-based index. Returns the existing tracked Page wrapper if one is found for the handle, preserving font and encoding state set on the original object.

Source
page_layout

the current setting for page layout.

Source
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.

Source
page_mode

the current setting for page mode.

Source
page_mode=(mode : PageMode)

sets how the document should be displayed.

Source
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.

Source
pdfa_conformance=(level : PDFAConformance)

Sets the PDF/A conformance level and generates the required XMP metadata block. Must be called before adding pages.

  • level the conformance level (see PDFAConformance).
Source
save_to_file(path : String)

saves the current document to a file.

Source
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.
Source
subject
Source
subject=(v : String)
Source
title
Source
title=(v : String)
Source
to_io

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

Source
to_unsafe
Source
use_cns_encodings

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

Source
use_cns_fonts

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

Source
use_cnt_encodings

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

Source
use_cnt_fonts

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

Source
use_jp_encodings

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

Source
use_jp_fonts

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

Source
use_kr_encodings

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

Source
use_kr_fonts

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

Source
use_utf_encodings

enables UTF-8 encoding support. After use_utf_encodings is invoked, the document can use UTF-8 encoded text with compatible encoders.

Source
viewer_preference

gets the viewer preferences bitmask for the document.

Source
viewer_preference=(preference : ViewerPreference)

sets the viewer preferences that control PDF viewer UI behaviour on open.

  • preference one or more ViewerPreference flags combined with |.
Source