1. General information
- Printing to a print template is triggered by buttons placed in an edit form or on a view page.
- Supported print template formats include files “xls”, “xlsx”, “doc”, “docx”, “html”, “txt”, “csv”, and “pdf”.
- NET Genium also processes templates “htm”, “xlsm”, “ods”, “docm” and “odt”.
- Microsoft Excel, Microsoft Word and “html” print templates support automatic conversion to “pdf” with the “Save as PDF” option of the button. An “html” template is converted to A4 pages with the margins given by the “Margins” option of the button. A margin that is not filled in is zero.
Details
- When an “htm” or “html” template is loaded, the opening tag “<span style='display:none'>” is removed (the content it hid is therefore displayed in the output) and line breaks (the CR LF character pair) outside the “<pre>” tag are replaced with a space and multiple spaces are merged into one – a single-line “//” comment in a script therefore comments out the whole rest of the script. A style that removes paragraph margins is inserted at the beginning of the document. When the result is opened in a new browser window without the “Save as PDF” option, a script that opens the print dialog is appended at the end, but it is not appended to a file sent by e-mail, saved as an attachment or created by a server function.
- If the template file is missing, “txt”, “csv” and “html” templates print an empty document. For other formats and for an “html” template with the “Save as PDF” option printing ends with an error.
2. Location and management of print templates
- Print templates must be stored in the directory “NETGenium\Templates”.
- Files can be copied into this directory directly on the server or via the NET Genium settings dialog on the Print templates tab.
- From this directory, print templates can be selected directly in the properties of the button that triggers printing.
3. Identifiers and variables in print templates
- Identifiers of database controls and script variables are used in print templates. In “txt”, “csv” and “html” templates, server functions and system variables are evaluated as well, but in Microsoft Excel and Microsoft Word templates they are not. Only functions and variables written in the template itself are evaluated – values substituted from records and variables are not evaluated as functions or variables, even if they contain such text. A function written in the template receives such a value as its argument. The result of the function is not evaluated as a function or a variable either.
- Script variables can be populated before printing using a script.
- At the position in the template where values are to be inserted, identifiers of database controls or variable names are used.
- NET Genium opens the print template, searches for all identifier occurrences, and replaces them with the corresponding values.
- A detailed description of variables used in print templates and control formatting options is given in the separate guide Variables.
3.1. Printing from an edit form
- Inserting values of database controls into a print template from the currently opened record in an edit form uses identifiers in the shortened format “#ng_control#”.
- A print button always saves the record before printing. It does not save it only in a read-only edit form.
3.2. Printing from view pages and tables
- All records matching the conditions of the view table and the search, filters and time period entered by the user are printed, in the user's sort order – not only the currently displayed page. Marked records are not taken into account when printing. The number of printed records is limited by the “Load only first” option of the database query.
Details
- When printing with a button, the restrictions of the view table display apply. If “Show search results only” is checked for the view table and the query does not have “Load only first” set, at most as many records are printed as set in “Records per page”. Without a search term no record is printed – in an edit form this does not apply if the query condition refers to a filled-in value of the form. A view table that is not currently displaying data – on a view page it is on a tab other than the one currently displayed or the user has hidden it, in an edit form it has “Hide data source” checked and the data is not currently displayed – is printed without records. These restrictions do not apply to printing with the “SCHEDULEPRINT” and “PRINTATTACHMENT” functions.
- Printing records from a view table requires the use of identifiers in full form, in the following possible formats:
3.2.1. First format (recommended)
- “#DG10#.#ng_control#”, “#DG11#.#ng_control#”, etc.
- Intended and most suitable for datagrids, using notation based on the datagrid ID (JavaScript element name).
- This approach is more stable when additional datagrids are added to a form and their order changes, which would cause identifier changes when using the second format.
- The number after the letters “DG” is the ID of the view table control.
3.2.2. Second format
- “#datagrid1#.#ng_control#”, “#datagrid2#.#ng_control#”, etc.
- This full identifier format, including the visual control type name and its sequence number, reflects scenarios where multiple view tables with the same data source but different filtering conditions are placed in an edit form or on a view page.
- Additional examples:
- “#calendar1#.#ng_control#”, “#calendar2#.#ng_control#”, etc.
- “#timetable1#.#ng_control#”, “#timetable2#.#ng_control#”, etc.
- “#planner1#.#ng_control#”, “#planner2#.#ng_control#”, etc.
- “#kanban1#.#ng_control#”, “#kanban2#.#ng_control#”, etc.
3.2.3. Third format
- “#ng_form#.#ng_control#”.
- Microsoft Word templates do not recognize this format – they use only the first and second formats.
3.2.4. Fourth format
- “#*#.#ng_control#”.
- It is used when printing from an edit form in “txt”, “csv”, “html” and Microsoft Excel templates and, in a Microsoft Excel template printed from a view page with the “1 record = 1 sheet” option, also for the nested tables of the form of each record. It does not identify a view table – if the form contains several view tables, the first or second format is used.
3.3. Print behavior when using full identifiers
- NET Genium detects the use of full identifiers and automatically inserts values of individual records one below another.
- If identifiers are placed next to each other on a single line, the print output is a table where each record occupies one row in the print template.
- If identifiers are placed next to each other on two lines, the print output is a table where each record consists of two lines in the print template.
- The repeated section differs by template format:
- “html” – only the table row (“<tr>”) containing the identifiers is repeated and identifiers outside a table row are not repeated. A fixed height given by the “height” attribute or the “height” style property, both written in lowercase, does not apply anywhere in the repeated row of the template – including images and elements inside its cells. The “line-height”, “min-height” and “max-height” properties are kept and heights in values substituted for identifiers are not modified,
- “txt” and “csv” – only the first section of consecutive lines with identifiers of the given table is repeated, and identifiers of the table on further lines of the template are not filled in with the values of its records,
- Microsoft Excel – a section of consecutive sheet rows is repeated,
- Microsoft Word – records are not repeated one below another. When printing from an edit form, the values of all records of the view table are inserted into one field separated by commas, unless the button has the “1 nested record = 1 sheet (XLS) / 1 document (DOC)” field checked – with it, each record of the nearest view table or calendar placed above the button (unless the table is statistical) is printed to a separate document and the documents are downloaded as a ZIP archive. When printing from a view page, each record is printed to a separate document and the documents are downloaded as a ZIP archive. When sent by e-mail, the documents are attached to the message one by one.
Details
- In a Microsoft Excel template, when a view table whose identifiers the sheet contains is filled in, every row whose cell in the first column contains exactly the text “remove” is deleted from the sheet. If the button has the “Insert new rows” value selected in the “Nested records” field, the row is not deleted. Instead, its first cell is emptied and the row is hidden by setting its height to zero.
3.4. Specific printing scenarios
- Printing from a view page that contains only a single view table does not require full identifiers. Simplified identifiers are sufficient.
- Printing from a view page to “pdf” and Microsoft Word prints each record of the first control of the “DataGrid”, “Calendar”, “TimeTable”, “Planner” or “Kanban” type to a separate file. A statistical view table is skipped. The files are downloaded as a ZIP archive. When sent by e-mail, they are attached to the message one by one.
Details
- If the view page contains none of the listed controls that can be printed this way, printing creates no file.
- If each record is printed to a separate file and the printed table contains no record, one file is printed as if the table contained one empty record.
- If each record is printed to a separate Microsoft Word document – from a view page, or from an edit form with the “1 nested record = 1 sheet (XLS) / 1 document (DOC)” field checked – identifiers of the first format (“#DG...#.”) can also be used for the view tables (DataGrid and LiteDataGrid) in the form of the printed record, except for tables with “Show search results only” checked. The values of all records of such a table are inserted into one field separated by commas.
- In a Microsoft Word document printed record by record, the values of the printed record are filled in only by identifiers in the shortened format “#ng_control#”. Identifiers in the full “#DG...#.” form referring to the printed view table are not filled in, and the “#datagridN#.” form refers to the nested tables of the form of the printed record. When printing from an edit form, the controls of this form are not available in the document – their values can be passed to the document in script variables.
- If a view page contains two or more view tables, all over the same edit form and none of them statistical, the button does not have the “1 record = 1 sheet” field checked and a Microsoft Excel template does not use identifiers of the first or second format (“#DG...#.”, “#datagrid...#.”, “#calendar...#.”), each table is printed to a separate copy of the sheet.
- The “1 nested record = 1 sheet (XLS) / 1 document (DOC)” field of an edit form button takes effect only when printing with a button that has a view table or a calendar placed above it. When printing with the “SCHEDULEPRINT” and “PRINTATTACHMENT” functions and with a button without such a control above it, the field keeps only one sheet in a Microsoft Excel template and prints a Microsoft Word template to a single document.
- Printing a statistical view table with aggregated columns uses column identifiers “c0”, “c1”, “c2”, etc., according to their order in the table header, numbered from zero.
3.5. Formatting values in print templates
- Print templates created in Microsoft Word must have identifiers placed in fields of type “MergeField”.
Details
- In NET Genium Online, a Microsoft Word template loads no remote resources – neither an image from an absolute URL in a value with HTML (e.g. in a “RichTextBox” control) nor an image linked in the template itself is inserted into the document. NET Genium attachments and images embedded as a “data URI” are inserted as usual.
- Values in print templates can be formatted using the syntax “#identifier@format#”.
- The value of a “ForeignKey” control is printed as the text of the referenced record only if the control has the “Show value in the view table without the need to join” field checked. Otherwise the ID of the referenced record is printed.
- In a Microsoft Excel template, a cell that contains only the identifier of a control with a numeric data type gets the value as a number. This does not apply to a “ForeignKey” control printed as text.
- In “csv” templates, HTML tags are removed from a value substituted for an identifier, a script variable or a print template variable (“#date1#”, “#date2#”, “#f0#”..., “#filters#” and “#identifiers#”), and if the value contains a semicolon or a line break, or begins or ends with a double quote, it is enclosed in double quotes and the double quotes inside it are doubled. Identifiers in a “csv” template are therefore not enclosed in quotes. Results of server functions and system variables are substituted unchanged – a value that may contain a semicolon, a line break or a double quote has to be handled in the template itself.
4. Images in print templates in the “html” format
- Print templates in the “html” format can contain images (typically a logo) inserted using the “<img>” tag, in one of the following three ways:
- Image embedded directly in the template as a base64-encoded “data URI”, e.g. <img src="data:image/png;base64,..."> – the template is self-contained and the image is printed both when printing to “html” and when converting to “pdf”. This is the recommended approach.
- Reference to a NET Genium attachment – the image is uploaded to NET Genium as an attachment, and the relative attachment address in the form <img src="Download.aspx?..."> (without the server address) is inserted into the template. When converting to “pdf”, NET Genium embeds the image into the document automatically. When printing to “html”, the image is loaded by the browser of the logged-in user.
- Absolute URL of a publicly available image, e.g. <img src="https://www.example.com/logo.png"> – when printing to “html”, the image is loaded by the browser of the user. In NET Genium Online it is not loaded when converting to “pdf”, because the conversion to “pdf” downloads no remote resources there.
- Relative references to files in the “NETGenium\Templates” directory, e.g. <img src="Templates/logo.png">, are not resolved when converting to “pdf”, and in NET Genium Online they do not work even when printing to “html”, because the “Templates” directory is not accessible via the browser in the cloud. Therefore, only the three approaches listed above are used.
5. Post-print events
- After successful generation of a print output, the external function “NETGenium.OnAfterPrint” is automatically executed, allowing modification of the output file name as well as its content or format.
Details
- If each record is printed to a separate file, the function is called for each record separately. An error in the function interrupts printing.
5.1. Rights and content of the printed document
- A print template prints every identifier it contains – including the value of a control the printing user has no right to. The content of the printed document is therefore determined by the author of the template, not by the rights of the printing user.
Details
- Value formatting (conversion to the displayed text, number and date format) is applied only to controls the user has rights to. Other identifiers are filled in with the value stored in the database.
- The value of a control with the “Show value” setting set to “only to the owner or co-owner of the record” that the printing user has the right to is printed for a user with administrator rights and for the owner and co-owner of the record, and for an unowned record for all users. For other users it is printed empty. The exceptions are the “RichTextBox”, “File” and “Image” controls in Microsoft Word templates and a cell of a Microsoft Excel template that contains only the identifier – there the value is always printed.
5.2. Encoding of text templates
- “txt”, “csv”, “htm” and “html” templates saved in UTF-8 with BOM are also printed to a file in UTF-8 with BOM. Templates without BOM are read and printed in the character set of the server (typically Windows-1250).
5.3. Name of the resulting file
- When printing with a button, the name of the resulting file consists of the button label and the extension of the resulting format. A ZIP archive with “pdf” documents printed from a view page is named after the last record. If the button label is the text “Print” in any NET Genium language (regardless of letter case), the name of the edit form or the view page being printed from is used instead.
- A name scheduled in the button script with the “SCHEDULEPRINTNAME” function replaces the button label. It does not apply to printing scheduled by the “SCHEDULEPRINT” and “SCHEDULEPRINTOFATTACHMENT” functions or to printing by the “PRINTATTACHMENT” function.
Details
- If each record is printed to a separate file, the files are named “Record 1”, “Record 2”, etc. The numbers are padded with zeros from the left to the same number of digits.
- Diacritics are removed from the name of a file sent by e-mail or saved as an attachment of a new record when printing opens an edit form (the “Open an edit form” option of the button or the “SCHEDULEFORM” function in its script) and from the names of the files in a ZIP archive. A downloaded file and an attachment printed by the “PRINTATTACHMENT” function keep them.
- A name scheduled with the “SCHEDULEPRINTNAME” function also applies to a document created with the “New from template” item of the “File” control. If it ends with the “.pdf” extension, the created document is converted to the PDF format. If a button of an edit form prints to a print template in the PDF format and the scheduled name ends with the “.htm” or “.html” extension, the result is printed in the HTML format.
- The file name can be changed in the “NETGenium.OnAfterPrint” external function.
6. Troubleshooting
6.1. Multiple blank pages printed from “xls” and “xlsx” templates
- If printing with an “xls” or “xlsx” print template produces multiple blank pages, it means the template content does not fit the page width and the overflowing columns are printed on separate pages.
- Solution: the print scale in the print template is set to 100% and the page margins are reduced so that the print fits the width of a single page. If needed, the font size and column widths are reduced as well.
6.2. Print area message for Microsoft Excel templates
- Printing to a Microsoft Excel template ends with the message “The print template has a print area defined. It is necessary to clear this area first.” if the sheet of the template that is printed to has a print area defined and at the same time:
- the button on a view page has the “1 record = 1 sheet” field checked, or the button in an edit form has the “1 nested record = 1 sheet (XLS) / 1 document (DOC)” field checked,
- or the view page printed from without this field checked contains two or more view tables, all over the same edit form – even if the template uses identifiers of the first or second format.
- Solution: the print area of this sheet is cleared in the print template.
7. Relation to other documentation
- A detailed description of variables used in print templates and control formatting options is given in the separate guide Variables.
- A detailed description of programming print templates in “pdf” format is provided in the separate guide Programming of print templates in PDF format.