Skip to content

PDFProvider.js

William edited this page May 3, 2024 · 9 revisions

PDFProvider

PDFProvider module

Author: William Leemans
Copyright: William Leemans 2024

PDFProvider.pdfProvider ⇐ baseProvider

A class to manipulate PDF files. Fills out forms created text boxes and adds images.

You should use:

  • pdfProvider.field to add form field text
  • pdfProvider.image to add images
  • pdfProvider.textBox to add text boxes

Kind: static class of PDFProvider
Extends: baseProvider
Requires: module:pdf-lib, module:fontkit

new exports.pdfProvider(actor)

Param Type Description
actor Object The Foundry VTT actor object.

pdfProvider.convertHexColorToRgb(hex) ⇒ array

Convert Hexadecimal color (HTML) to RGB

Kind: instance method of pdfProvider
Returns: array - the RGB color code in % for each of the main colors, or the color code for black

Param Type Description
hex string The hexadecimal value (html color) representing a color

pdfProvider.createFile() ⇒ undefined

Create a file when no file is associated with the provider sheet

Kind: instance method of pdfProvider

pdfProvider.defaultFont(font, size, lineHeight)

Set the default font, size and lineheight for text boxes

Kind: instance method of pdfProvider

Param Type Description
font string The name of the font file
size number The size of the font, expressed in % of the page height.
lineHeight number The lineheight of the font, expressed in % of the page height.

pdfProvider.defaultFontColor(color)

Set the default font color for text boxes

Kind: instance method of pdfProvider

Param Type Description
color string | rgb an HTML or fontkit rgb color.

pdfProvider.embedImage(pdf, imageData)

Embed an image to the given PDF Object

Kind: instance method of pdfProvider

Param Type Description
pdf PDFDocument The PDFDocument to add the image to
imageData Object an Object containing path, (x,y) coordinates, scaling information, etc...

pdfProvider.embedFont(pdf, fileName) ⇒ CustomFontEmbedder

Embed a fontfile into the given PDF

Kind: instance method of pdfProvider
Returns: CustomFontEmbedder - The resulted embedded font

Param Type Description
pdf PDFDocument The pdf to embed the font into
fileName string the name of the file

pdfProvider.embedTextBox(pdf, textBoxData)

Embed a Text Box to the given PDF Object

Kind: instance method of pdfProvider

Param Type Description
pdf PDFDocument The PDFDocument to add the Text Box to
textBoxData Object an Object containing path, (x,y) coordinates, scaling information, etc...

pdfProvider.field(file, name, value, options)

Store form field information, text for text fields, booleans for checkboxes

Kind: instance method of pdfProvider

Param Type Description
file string the pdf filename to apply the image to ('all' means all PDFs)
name string The name of the field to reference
value string | boolean | Promise The value to be applied to the field
options Object Optional data, functions for the field
options.parseValue function function to parse the value after resolving the value Promise

pdfProvider.fieldExists(file, name) ⇒ boolean

Check if a field is defined

Kind: instance method of pdfProvider
Returns: boolean - whether the field is defined

Param Type Description
file string The name of the PDF file a field belongs to
name string The name of the field to return the value from

pdfProvider.getField(file, name) ⇒ string | boolean | undefined

Return a PDF field object definition

Kind: instance method of pdfProvider
Returns: string | boolean | undefined - the requested field data

Param Type Description
file string The name of the PDF file a field belongs to
name string The name of the field to return the value from

pdfProvider.getFieldValue(file, name, defaultValue) ⇒ string | boolean

Return a PDF field value

Kind: instance method of pdfProvider
Returns: string | boolean - - the requested field

Param Type Description
file string The name of the PDF file a field belongs to
name string The name of the field to return the value from
defaultValue string | boolean The value to return if the field cannot be found

pdfProvider.getFieldOptions(file, name, optionName) ⇒ Object | string | boolean | number

Kind: instance method of pdfProvider
Returns: Object | string | boolean | number - the requested field option(s)

Param Type Description
file string The name of the PDF file a field belongs to
name string The name of the field to return the value from
optionName string Optional name of a field to return

pdfProvider.image(file, page, x, y, path, maxWidth, maxHeight, options)

Store image information

Kind: instance method of pdfProvider

Param Type Description
file string | array the pdf filename(s) to apply the image to ('all' means all PDFs)
page number | array The page(s) to add the image to
x number The x coordinate for the image
y number The y coordinate for the image
path string The url to the image to add
maxWidth number The maximum width of the Image
maxHeight number The maximum height of the Image
options object Additional options

pdfProvider.updateFile()

Parses the provided filedata, fills out pdf form fields, adds images and saves the blob

Kind: instance method of pdfProvider

pdfProvider.parseValue(value, parser) ⇒

asynchronously parse a value through a function

Kind: instance method of pdfProvider
Returns: the parsed value

Param Type Description
value any | Promise the value to be parsed
parser function the parser function, arguments passed are the (awaited) value and this

pdfProvider.textBox(reference, file, page, x, y, width, height, text, options)

Store pdf text box information

Kind: instance method of pdfProvider

Param Type Description
reference string a reference for the textBox added (for debugging purposes)
file string | array the pdf filename(s) to apply the text box to ('all' means all PDFs)
page number | array The page(s) to add the text box to
x number The x coordinate for the text box (in % of the page width)
y number The y coordinate for the text box (in % of the page height)
width number The width of the text box to add (in % of the page width)
height number The height of the text box to add (in % of the page height)
text string | Promise The text to display in the text box
options object Additional options
options.color string | array either a hexadecimal (html) color, or array of rgb values (in %)
options.font string The filename of the font used, will be looked for in the provider's font directory or the module's font directory
options.halign string how to align the text horizontally (left[default], center, right)
options.lineHeight number The lineheight of the text (in % of the page height)
options.multiline boolean treat this text as multiline (default: true)
options.overflow boolean allow the text to overflow the boundaries (default: false) The text will be suffixed with three dots if it is too long
options.prefix string a text to add at the beginning of @param text
options.size number The font size
options.suffix string a text to add at the end of @param text, after any overflow characters.
options.valign string how to align the text vertically (top, center, bottom[default])
options.valueParser function function to parse the value after resolving the value Promise