PDF Document

Creates a PDF document by inserting database content into an RTF template and then converting the RTF to a PDF.

This component has a database interface identical to the RTF Component.

Component modes: Standard | Label | Static PDF

Component mode: Static PDF

File modes: Download | Save | Save and download | Batch

File mode: Download

The user downloads the file.

Custom fonts

The component will attempt to embed fonts used in the document template into the final PDF file. For this to work you will first need to install the fonts on the web server.

Tags in the template

The RTF template can contain the following tags:

<#XXX#>
Normal tag that is replaced by the value in the column with the same name, i.e. XXX. Values such as dates and decimals should be formatted in the procedure and returned as varchars for consistent formatting. Tags can also contain special data like images (see further below).

All values are treated as RTF literals. That means that you can emit, for example, a \page command directly into the document. It also means that you need to use the SoftadminUtil.RTF_Escape function if you need to emit strings containing characters like '{', '}' or ''.

<#$$XXX#>...</#$$XXX#>
Section that surrounds a normal tag. If the value in the column XXX is NULL or missing then the entire section is skipped from the final document.

<##YYY##>...</##YYY##>
Container that is repeated 0-n times depending on the resultset. Each normal tag within the container corresponds to a column in a resultset.

<$$##YYY##>...</$$##YYY##>
Section that surrounds a container (with the same name). If zero rows are returned from the container SQL-statement, then the entire section is skipped from the final document.

Images

For any normal tag where the database returns varbinary data, the data will be assumed to be either a PNG or JPG image and inserted into the document as such. The document will use the image's original resolution when printed.

QR Codes

For any normal tag where the database returns XML data with the root element QrCode a QR Code will be rendered and inserted into the document.

<QrCode cm="3.14">MyQrCode</QrCode>

The element accepts a single (optional) attribute, cm which specifies how large the code should be in centimeters. Do note that this size includes the mandatory quiet zone surrounding the QR code.

Swiss QR Codes

The Swiss QR Code symbology has been developed in Switzerland to automate digital payments. All payment receipts and bills in Switzerland are intended to have a Swiss QR Code barcode that encrypts payment details. To distinguish from basic QR Code, Swiss QR Code labels have the Swiss cross sign in the center.

Example-XML that will render a Swiss QR Code:

<QrCode Type="Swiss">
	<Currency>EUR</Currency>
	<Iban>CH4431999123000889012</Iban>
	<Creditor>
		<AddressType>S</AddressType>
		<Name>John Doe</Name>
		<ZipCode>3003</ZipCode>
		<City>Bern</City>
		<Country>CH</Country>
		<Street>Parlamentsgebäude</Street>
		<HouseNumber>1</HouseNumber>
	</Creditor>
	<Reference>210000000003139471430009017</Reference>
	<AdditionalInformation>
		<UnstructuredMessage>An unstructured message.</UnstructuredMessage>
		<BillInformation>Some bill information here.</BillInformation>
	</AdditionalInformation>
	<Amount>136.19</Amount>
	<Debitor>
		<AddressType>K</AddressType>
		<Name>Multisoft AB</Name>
		<Country>SE</Country>
		<AddressLine1>Brunkebergstorg 5</AddressLine1>
		<AddressLine2>11151 Stockholm</AddressLine2>
	</Debitor>
	<RequestedDateOfPayment>2022-12-30</RequestedDateOfPayment>
</QrCode>

The tags Currency, Iban and Creditor are mandatory. Contact address types are "S" for structured address and "K" for combined address (kombinierte Adresse).

Component settings

Clean RTF Template
If on, tags are cleaned, container consistency is checked and corrected and spaces may be inserted between rtf control words and tags if neccessary to keep them separated after the tag is replaced with contents.
Process HTML
When activated supplied data is processed and HTML-tags for basic formatting are translated to corresponding RTF-formatting.

The affected HTML-tags are: <b> <i> <u> <strong> <em> <strike> <br> <tab> <s> <del>

SQL

SQL Call: Init (mandatory)

May modify database: No

Parameters

@Action string
Will be set to 'Init'

Resultset: Extra parameters (optional)

Parameters that will be sent to the following calls.
Table count: repeated zero or one time
Row count: zero or more rows
Columns
<xxx> mandatory string
An extra parameter. The name of this column is used as the parameter name. The table should contain one column for each parameter.

Resultset: Filename (optional)

Table count: repeated zero or one time
Row count: exactly one row
Columns
FileName mandatory string
Name of the downloaded file.

SQL Call: Retrieve rtf template (mandatory)

Sql to retrieve RTF template from database.

May modify database: No

Parameters

@template bit
Set to '1' by Softadmin® when the component asks for an rtf template.

Resultset: Rtf template

Table count: repeated exactly once
Row count: exactly one row
Columns
xxx mandatory binary
Rtf template
xxxfilename optional string
Filename

SQL Call: Get templates (mandatory)

Retrieves the PDF templates and fonts that may be used to generate the document.

May modify database: No

Parameters

@Action string
Will be set to 'GetTemplates'.

Resultset: PDF templates

Table count: repeated exactly once
Row count: one or more rows
Columns
TemplateId mandatory string

The id of the template.

TemplateData mandatory binary

Binary data of the template.

Resultset: Fonts (optional)

Table count: repeated zero or one time
Row count: zero or more rows
Columns
FontId mandatory string

Id of the font.

FontData mandatory binary

Binary data of the font.

SQL Call: Retrieve document data (mandatory)

SQL to retrieve information to fill the RTF template, this SQL is called first once, without the @container parameter set, then once for each container-tag.

May modify database: No

Parameters

@<DocumentId> string
Used in batch mode. This parameter will have the same name as the first column in the batch documents result set of the Retrieve rtf template call, and contains the id value of the document.
@container string
Used if RTF template contains a container-tag (<##XXX##>...). The container tag is then repeated as many times as there are rows in the resultset.
@id string
Used if RTF template contains a container-tag within another container-tag (<##XXX##>..<##YYY##>....). The 'id' value is the first column in the outer resultset (in this case, the resultset generated by the <##XXX##>-tag).

Resultset: Data to fill document

Resultset that contains information to fill the RTF template with, each column name corresponds to a tag in the template (<#XXX#>). At least one row must be returned when @container is not set.

Images:
If the column is a varbinary column it is assumed to be a picture and is inserted as a picture in the document. Note that pictures in rtf documents are very large and it is therefore recommended that this feature is used with moderation.

QR Codes:
If the column contains the XML <QrCode cm="xxx">data</QrCode> then a QR code with the given data and size is inserted. Do note that the size includes the quiet zone around the QR code.
Table count: repeated exactly once
Row count: zero or more rows
Columns
xxx optional string
Replaces the tag <#xxx#>.

SQL Call: Create document (mandatory)

Call to create the finished document from the templates.

May modify database: No

Parameters

@Action string
Will be set to 'CreateDocument'.

Resultset: Page info

Determines which pages from the templates should be used and in what order. The pages will be added to the finished document based on how the table is sorted.

Table count: repeated exactly once
Row count: zero or more rows
Columns
TemplateId mandatory string

The id of the template to use pages from.

PageNumber optional int

Which page to use from the template.

Default: NULL means use the whole document.

Resultset: Text (optional)

The text that should be inserted in the finished document.

Table count: repeated zero or one time
Row count: zero or more rows
Columns
Text mandatory string

The text that should be inserted.

PageNumber mandatory int

Which page of the finished document that the text should be inserted into.

FontId mandatory string

Id of the font to use.

X mandatory decimal

X-coordinate that the text should be inserted at.

Y mandatory decimal

Y-coordinate that the text should be inserted at.

FontSize mandatory decimal

Font size of the text.

LineHeight optional decimal

Line height of the text. If not specified, the component will try its best to calculate it from the font.

TextWidth optional decimal

Maximum width of the text. Determines when line breaks are inserted.

Resultset: Images (optional)

Images to insert in the document. The image data will be retreived in a later call.

Table count: repeated zero or one time
Row count: zero or more rows
Columns
ImageId mandatory string

The id of the image.

PageNumber mandatory int

Which page of the finished document that the image should be inserted into.

X mandatory decimal

X-coordinate that the image should be inserted at.

Y mandatory decimal

Y-coordinate that the image should be inserted at.

Width optional decimal

Width of the inserted image. Images will always retain its aspect ratio.

Height optional decimal

Height of the inserted image. Images will always retain its aspect ratio.

SQL Call: Get Image

Will be called once for each unique image.

May modify database: No

Parameters

@Action string
Will be set to 'GetImage'.
@ImageId string
Id of the image to retrieve.

Resultset: Image data

Table count: repeated exactly once
Row count: exactly one row
Columns
<xxx> mandatory binary

Binary data of the image.

SQL Call: Forward to next menu item

You do not need to implement this call. It is only made if the menu item has dynamic forwarding.

May modify database: No

Parameters

@forward bit
Set to '1' by Softadmin® when the component wants to know where to forward to.

Resultset: Forwarding definitions (optional)

Table count: repeated zero or more times
Row count: exactly one row
Columns
ADMIN_ErrorMessage optional string

Displays a user friendly error message to the user. This blocks any forwarding for the user.

ADMIN_ConfirmDelete optional string

Displays a user friendly confirmation message to the user with a delete button as the confirm button. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Delete'.

ADMIN_ConfirmQuestion optional string

Displays a user friendly confirmation message to the user with a question style. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Confirm'.

ADMIN_ConfirmWarning optional string

Displays a user friendly confirmation message to the user with a warning style. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Confirm'.

ADMIN_InfoMessage optional string

Displays a user friendly info message to the user. When the user clicks OK the user is forwarded.

ADMIN_SuccessMessage optional string

Displays a user friendly success message to the user. When the user clicks OK the user is forwarded.

ADMIN_Dialog optional string

The dialog alias of a predefined dialog to show the user. Must be the first column in the result set table. Use multiple result set tables to combine with other forwarding.

Use the menu item "Admin > Dialogs" to register new dialogs or find aliases for existing ones.

<xxx> (for ADMIN_Dialog) optional any

Any column without special meaning in the result set with the first column ADMIN_Dialog will be used to make replacements of placeholders in the message and title text.

ADMIN_DebugInfo optional string

Additional information to show the developer when using ADMIN_Dialog.

<passing_field> optional string
Any column with no other specific meaning will pe passed along to the menu item you are forwarding to.
ADMIN_CancelCidStepsBack optional int
Number of steps in the page history to jump back if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). This value overrides cancelcidstepsback specified in the query string.
ADMIN_CancelReturnToMenuItem optional string
Jumps back to the menu item with this alias if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). If no prior menu item is found with the given alias, then an error is thrown.
ADMIN_CancelMenuItem optional string
Alias of the menu item to execute if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). This value overrides cancelmenuitemid specified in the query string.
ADMIN_CancelMenuItemId optional int

Deprecated. Use ADMIN_CancelMenuItem instead.

Id of the menuitem to execute if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). This value overrides cancelmenuitemid specified in the query string.

ADMIN_CidStepsBack optional int
Number of steps in the page history to jump back after execution (the default being one step back). This value overrides any destination specified by the query string.
ADMIN_ReturnToMenuItem optional string
Jumps back to the menu item with this alias after execution. This value overrides any destination specified by the query string. If no prior menu item is found with the given alias, then an error is thrown.
ADMIN_Force optional string
Prompts the user with the specified text and the user may answer OK or cancel. If the user chooses OK the sql call will be rerun with the parameter @force set to 1.
ADMIN_Forward optional string
Displays a user friendly message and then forwards to the next menu item.
ADMIN_ForwardLink optional string
Alias of the link to forward to.
ADMIN_ForwardMenuGroup optional string

Alias of the menu group to show after execution (instead of former menu item). This value overrides any destination specified by the query string.

ADMIN_ForwardMenuGroupId optional int

Deprecated. Use ADMIN_ForwardMenuGroup instead.

Id of the menu group to show after execution (instead of former menu item). This value overrides any destination specified by the query string.

ADMIN_ForwardMenuItem optional string
Alias of the menu item to execute after execution (instead of former menu item). This value overrides any destination specified by the query string.
ADMIN_ForwardMenuItemId optional int

Deprecated. Use ADMIN_ForwardMenuItem instead.

Id of the menu item to execute after execution (instead of former menu item). This value overrides any destination specified by the query string.

ADMIN_Message optional string

Displays a user friendly error message to the user.

ADMIN_PasteHtmlFromPopup optional string
Pastes HTML into an HTML editor. See ADMIN_SetFieldValueFromPopup.
ADMIN_SetFieldValueFromPopup optional string
Sets the value of the field specified in the menuitempopup call. Only select this column if menu item is opened in a popup.
ADMIN_ClosePopup optional bit
If this column is anything but NULL the popup will be closed. Only select this column if the menu item is opened in a popup.
Default: The default behavior is to step back inside the popup window and close it if there is nothing to step back to.
ADMIN_ClosePopupAndReloadOpener optional bit

If this column is anything but NULL the popup will be closed and the parent will be reloaded. Only select this column if the menu item is opened in a popup. Avoid using this feature if the opener is a newEdit as that may interrupt the user's ongoing input.

ADMIN_ClearHistory optional any
When the value is not NULL all navigation history is cleared and the user can't navigate back. This is only supported when forwarding to another menu item.
ADMIN_RefreshMenu optional bit
Will trigger a reload of the sidebar if the column is anything but NULL.
Cache optional string
Cache key to be cleared. Supports wildcards.
CacheUserId optional string
Either a user id or '%'.

Clears all caches (e.g. access permissions) related to the specified user id.

Use '%' to clear caches for all users.
CancelButtonText optional string

Changes the text of the Cancel button when used with ADMIN_Force, ADMIN_ConfirmWarning, ADMIN_ConfirmQuestion, ADMIN_ConfirmDelete.

OkButtonText optional string

Changes the text of the OK button when used with ADMIN_ErrorMessage, ADMIN_ConfirmWarning, ADMIN_ConfirmQuestion, ADMIN_ConfirmDelete, ADMIN_InfoMessage, ADMIN_SuccessMessage, ADMIN_Message, ADMIN_Force, or ADMIN_Forward. ADMIN_Force,

SQL Call: Validate parameters

Allows you to validate the SQL parameters before any other SQL is run in the component. This call is only made if the SQL is a stored procedure and Validate parameters is checked.

May modify database: No

Parameters

@Force bit
Set to 1 if the last call to validate parameters used admin_force and the user clicked OK in the OK/Cancel dialog.
@ValidateParams bit
Set to 1 when this call is made.

Resultset: Messages (optional)

Table count: repeated zero or one time
Row count: zero or one row
Columns
ADMIN_Force optional string
Message asking the end user to confirm their parameters.
ADMIN_Message optional string
Message explaining why the parameters are rejected.

Custom access control and logging

SQL Call: Custom access control and logging

Use this call to restrict which entries a user is allowed to view and edit, and to log which entries a user views.

Access to a menu item is normally controlled through functions and roles alone but some entities need more fine grained control. For example, a user may have access to the View Member menu item for normal members but not for members with a protected identity.

The menu items a user visits are always logged (in ADMINLogMenuItem) but for sensitive data you may need to log exactly what entries are viewed. Do the logging in this call as the common ways of viewing data (grid and InfoSQL) are not allowed to modify the database.

If you bind a scalar function instead of a stored procedure to this call then its name must end with '_GrantAccess'.

May modify database: Yes

Resultset: Access permissions

Return whether the user is allowed to visit the menu item with the current parameters.
Table count: repeated exactly once
Row count: exactly one row
Columns
GrantAccess mandatory bit
1 if the user is allowed to view the menu item, 0 if the user should not be allowed to view the menu item.

If 0 then an error will be logged as the user should not have been able to reach the menu item with the given parameters in the first place.

Examples

Informing the user of invalid container names

If you allow end users to administer their own templates then they will occassionally create templates with misspelled or completely unsupported containers. You can use RAISERROR prefixed with ADMIN_MESSAGE to display an error message to the user trying to use the broken template.

ALTER PROCEDURE Examples.RtfWithInvalidContainers
	@Template  bit = 0,
	@Container sysname = NULL
AS
BEGIN
	IF @Container IS NOT NULL
	BEGIN
		IF @Container = 'Container1'
		BEGIN
			SELECT 1 AS ValueInContainer;
			RETURN 0;
		END;

		IF @Container = 'Container2'
		BEGIN
			SELECT 2 AS ValueInContainer;
			RETURN 0;
		END;

		DECLARE @TemplateName varchar(300) = 'ExampleTemplate';

		RAISERROR('ADMIN_MESSAGEThe template %s contains an unsupported container %s.', 16, 1, @TemplateName, @Container);
		RETURN -1;
	END;

Best practice

Create template

Keep it simple, if the needed features are supported, create/save the template in WordPad instead of Word to get a less bloated template. This makes the documents smaller, faster to generate and less prone to errors.

Tables

When creating tables with a container tag the start container should be put in column 1 of the table, and the end container tag should be put after the table.

This should result in this order of data in the rtf file:
\trowd
\pard\intbl <##Container##>\cell\row
\pard </##Container##>