myDBR allows report data to be exported to various formats: Excel, PDF, CSV, JSON, and PHP serialized. Export options exist for extra functionality for the exports.
Exporting a report requires adding &export=format
to the URL or using a magic report parameter inExportFormatSet
. Supported export formats are:
xls
- Excel format (old).
xlsx
- Excel format (new). Requires zip support in PHP.
pdf
- Portable Document Format (PDF) (using wkhtmltopdf).
csv
- CSV format.
json
- the result is delivered as a JSON. If the report contains multiple result sets, the resulting array is a multidimensional JSON. Only basic result sets are supported (no support for charts, cross-tables, etc).
This export-format is meant for exchanging data between systems.
xml
- the result is delivered as XML. Only basic result sets are supported (no support for charts, cross tables etc).
This export-format is meant for exchanging data between systems.
sql
- the result is delivered as SQL-commands. One can optionally skip the myDBR commands adding parameter '&skip_mydbr_cmds=1' to the URL.
php_serialized
- the result is delivered as a serialized PHP-array. Supports only one result set. Only basic result sets are supported (no support for charts, cross-tables, etc).
This export-format is meant for exchanging data between systems.
For JSON data, additional URL parameters can be used to define the JSON format used.
default
- json array
&json_force_object=1
- json object
&json_object_array=1
- json object array
dbr.export.options
- Set export optiondbr.wkhtmltopdf
- Pass command line parameters to wkhtmltopdf commanddbr.calc.excel
- Define an Excel native formula for a columndbr.blob
- Read file from the databasedbr.export.json
- Keeps the JSON formatted column as JSON in JSON export
select 'dbr.export.options', option, value
select 'dbr.wkhtmltopdf', command_line_parameters
select 'dbr.calc.excel', ColumnReference, excel_formula
select 'dbr.blob', file_extension, filename
select 'dbr.export.json', ColRef [, ColRef...]]
Export options
orientation
- Set page orientation ('landscape', 'portrait') in PDF and Excel. Use this in the first result set only. The export document can have only one orientation
paper_size
- Set page size PDF and Excel. See the list below for different options
zoom_scale
- Set PDF/Excel zoom scale (value 50 is 50% zoom)
gridlines
- Disable/enable Excel gridlines
font
- Set font used in Excel
font_size
- Set font size in pixels used in Excel
autosize
- Turn Excel autosize on / off. Will try to fit the output into a cell
pdf
- With value 'weasyprint', myDBR uses weasyprint PDF generation insted of wkhtmltopdf
stylesheet, file
- Override default user/weasyprint.css
. File is relative to myDBR root
pagebreak
- Will produce a page break in PDF document and in the Excel worksheet
worksheet
- Will create a new worksheet with a given name (value) in Excel
position
- Will place the object into a given position. Example position is "C6".
disable
- Disable the chosen export types. Will get a comma-separated list of disabled export types (pdf, excel)
direct_mode
- Reduce memory consumption and speeds up export by using reduced functionality for Excel and CSV (suitable for plain tables).
debug
- Will display wkhtmltopdf command and the generated HTML file for debugging possible errors in user-generated HTML as wkhtmltopdf may fail to produce PDF from erroneous HTML
direct_mode
- Will produce faster exports by directly outputting row-by-row while skipping most of the myDBR features. Suitable for large direct query exports
header.title
- Define PDF export header title
header.title.style
- Define PDF export header title CSS style
include_image
- Defines whether charts are included in the Excel export
csv.delimiter
- Allows setting a CSV delimiter on a per report basis
csv.decimal_point
- Allows setting a CSV decimal_point on a per report basis
csv.enclosure
- Allows setting a CSV enclosure on a per report basis
csv.date_format
- Allows setting a CSV date_format on a per report basis
csv.time_format
- Allows setting a set CSV time_format on a per report basis
csv.charset
- Allows setting CSV charset on a per report basis
csv.line_ending
- Allows setting CSV line_ending on a per report basis
csv.header
- Allows setting CSV header visibility on a per report basis
csv.enclose_string_with_leading_or_trailing_space
- Allows setting CSV export quotes strings with leading or trailing_space
csv.linefeed_between_resultsets
- If the report contains multiple result sets, this will determine if the result sets are separated with linefeed
csv.use_bom_in_utf
- Use BOM (Byte order mark) in the CSV file
csv.skip_formatting
- Ignore column formatting in CSV export
csv.strip_tags
- Strip tags in csv export
excel.aggregate_formula, 1/0
- Enable/disable Excel native formulas in aggregate summary rows
xml.declaration
- Define the XML declaration
xml.header
- Define additional header tags
xml.root_tag
- Defines the XML root tag. If not defined, a generic "report" is used
xml.tag_attributes comma_separated_colreflist
- Turns values to data attributes. The parameter is a comma-separated list of column references
xml.element_wrapper column_to_wrap, wrapper_tag, wrapper_attributes, replacement_tag
- Wraps element with new element
xml.compatibility_mode, 1
- Use old, pre myDBR 4.0, XML format
filename
- define the export file within the report
colwidths
- Define the Excel column widths in pixels
formula
- Define an Excel formula in a cell. Format: 'formula:A2'[, 'colsyle'] or 'formula:ColumnReference'[, 'colsyle']
position.row row
- Set Excel export row for result set (select 'dbr.export.options', 'position.row', 1;)
position.column Column
- Set Excel export column for result set (select 'dbr.export.options', 'position.column', 'E';)
position.row.add nbr_of_rows
- Move result set nbr_of_rows-rows lower (select 'dbr.export.options', 'position.row.add', 3;)
position.column.add nbr_of_columns
- Move result set nbr_of_column-columns to right (select 'dbr.export.options', 'position.column.add', 2;)
tab_title.hide
- Do not show tab titles in Excel export. Use this in first result set only.
content-disposition.xxx, 'attachment' | 'inline'
- Set export Content-Disposition header to either 'attachment' or 'inline'. You can also use URL parameter 'content-disposition' with same options. Determines whether the report will be shown in the browser (inline) or downloaded as a file (attachment).
See also defaults.php
for more options.
Available page sizes for PDF (wkhtmltopdf) export:
Note that you can also use wkhtmltopdf options --page-height and --page-width to define a custom page size.
Available page sizes for Excel export:
myDBR supports wkhtmltopdf if installed on the server. wkhtmltopdf uses the Webkit rendering engine to convert content to PDF. Please see Optional installations on how to install wkhtmltopdf into your server.
When wkhtmltopdf is installed, PDF exports will support all HTML and JavaScript code generated (even with dbr.html).
By default, wkhtmltopdf export uses the header defined by the mydbr/user/export_header.php
as '--header-html' option in wkhtmltopdf. If this option is overridden with a parameter to 'dbr.wkhtmltopdf', a defined export header will be used. If you leave the '--header-html' -option as empty, no header is used.
myDBR supports WeasyPrint if installed on the server. WeasyPrint is visual rendering engine for HTML and CSS that can export to PDF. Please see the WeasyPrint documentation on how to install WeasyPrint into your server.
WeasyPrint handled purely the HTML to PDF conversion. It does not run the JavaScript code.
WeasyPrint headers and footers are determined by a CSS file (by default mydbr/user/weasyprint.css
, you can override it with the stylesheet
option).
select 'dbr.export.options', 'orientation', 'landscape'; select 'dbr.export.options', 'paper_size', 'A3_EXTRA_PAPER'; select 'dbr.export.options', 'zoom_scale', 50; select 'dbr.export.options', 'font', 'Verdana'; select 'dbr.export.options', 'font_size', 11; select 'dbr.export.options', 'autosize', 0; /* Turn off autosize */ select 'dbr.export.options', 'pagebreak', 1; select 'dbr.export.options', 'disable', 'pdf,excel'; select 'dbr.wkhtmltopdf', "--header-html ''"; /* Do not use header in export using wkhtmltopdf */
The report format can also be controlled with the magic parameter inExportFormatSet
. This will let the user to choose the report output format. No need for any changes in the report code.
create procedure sp_DBR_production_summary`( inCategory tinyint, inExportFormatSet varchar(4) ) begin -- ...report content end
You can use Excel calculations in Excel export.
select 'dbr.calc', 'total', '[A]*[B]'; select 'dbr.calc.excel', 'total', '=indirect(address(row(),column()-2))*indirect(address(row(),column()-1))'; select 10 as 'A', 20 as 'B', null as 'total';
The export feature may use a lot of memory, especially with complex Excel documents. If you run into problems with export functionality, try to increase the 'memory_limit'-parameter in php.ini. See the error message about the required memory.
The generated XML file will include all the result sets of the query. An example XML report and output:
select 'dbr.export.options', 'xml.declaration', '<?xml version="1.0" encoding="utf-8"?>'; select 'dbr.export.options', 'xml.root_tag', 'dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="filmlist.xsd"'; /* Will define the result set name in XML */ select 'dbr.resultclass', 'films'; /* Use these two columns as attributes */ select 'dbr.export.options', 'xml.tag_attributes', 'id', 'release_year'; /* Name the row in XML */ select 'dbr.rowclass', 'rowclass'; /* Do not show the rowname as data */ select 'dbr.hidecolumn', 'rowclass'; select 'dbr.export.options', 'xml.element_wrapper', 'pr_phone', 'phone', 'type="PRPhone"', 'phonenumber'; select 'dbr.export.options', 'xml.element_wrapper', 'production_phone', 'phone', 'type="ProductionPhone"', 'phonenumber'; select film_id as 'id', title as 'Title[filmtitle]', description, release_year, last_update, 'film' as 'rowclass', pr_phone, production_phone from mydata.film;
You can define the result set's name with the dbr.subtitle
command. If none is given, myDBR will generate one. Column attribute
names and data types are defined by the report. In the example, the column name 'title' has been changed to 'filmtitle'.
<?xml version="1.0" encoding="utf-8"?> <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="filmlist.xsd"> <film id="1" release_year="2006"> <filmtitle>ACADEMY DINOSAUR</filmtitle> <description> A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockeless </description> <last_update>2010-08-18T10:22:10</last_update> </film> <film id="2" release_year="2007"> <filmtitle>AMERICAN BACON</filmtitle> <description>Newly released film</description> <last_update>2010-09-23T15:22:55</last_update> <phone type="PRPhone"> <phonenumber>+1-202-555-0120</phonenumber> </phone> <phone type="ProductionPhone"> <phonenumber>+1-202-555-0156</phonenumber> </phone> </film> </dataroot>
By default, a myDBR report uses JSON array as JSON format. You can use change the default format with extra URL parameters json_force_object
and json_object_array
. To change the default JSON format to object format add the following line to user/defaults.php
.
$mydbr_defaults['export']['json']['force_object'] = true;
Sample report:
select film_id, title, release_year from film f limit 0,2;
The default JSON export format is a JSON array:
[ [ 1, "ALAMO VIDEOTAPE", 2006 ], [ 1, "ALASKA PHANTOM", 2006 ] ]
With &json_object_array=1
is added to the URL, the format is JSON object format:
{ "0": { "film_id":1, "title":"ALAMO VIDEOTAPE", "release_year":2006 }, "1": { "film_id":2, "title":"ALASKA PHANTOM", "release_year":2006 } }
With &json_force_object=1
is added to the URL, the format is JSON object array format:
[ { "film_id":1, "title":"ALAMO VIDEOTAPE", "release_year":2006 }, { "film_id":2, "title":"ALASKA PHANTOM", "release_year":2006 } ]
myDBR keeps the JSON columns in the query allowing to create hierarchical JSON. The MySQL/MariaDB query:
select f.film_id, f.title, JSON_ARRAYAGG(JSON_OBJECT('id', fa.actor_id, 'name', concat(a.first_name,' ', a.last_name))) as 'actors' from film f join film_actor fa on fa.film_id=f.film_id join actor a on a.actor_id=fa.actor_id where f.film_id between 2 and 3 group by f.film_id, f.title;
Will produce JSON:
{ "0": { "film_id": 2, "title": "AMERICAN BACON'abc", "actors": { "0": { "id": 19, "name": "BOB FAWCETT" }, "1": { "id": 85, "name": "MINNIE ZELLWEGER" } } }, "1": { "film_id": 3, "title": "ADAPTATION HOLES", "actors": { "0": { "id": 2, "name": "NICK WAHLBERG" }, "1": { "id": 19, "name": "BOB FAWCETT" }, "2": { "id": 24, "name": "CAMERON STREEP" } } } }
SQL Server and Sybase do not have JSON data types but can use JSON and convert data to JSON format. To take advantage of JSON functionality, one needs to tell myDBR that the result set will be JSON. (The optional 'format' parameter for json-option formats the JSON when displayed in HTML).
select 'dbr.resultset.options', 'json', 'format' select co.*, ci.* from country co join city ci on ci.CountryCode=co.Code where co.name in ('United States', 'Netherlands') for JSON AUTO
Will produce JSON:
[ { "Code": "NLD", "Name": "Netherlands", "Continent": "Europe", "Region": "Western Europe", "SurfaceArea": 41526, "IndepYear": 1581, "Population": 15864000, "LifeExpectancy": 78.3, "GNP": 371362, "GNPOld": 360478, "LocalName": "Nederland", "GovernmentForm": "Constitutional Monarchy", "HeadOfState": "Beatrix", "Capital": 5, "Code2": "NL", "ci": [ { "ID": 5, "Name": "Amsterdam", "CountryCode": "NLD", "District": "Noord-Holland", "Population": 731200 }, { "ID": 6, "Name": "Rotterdam", ...
With formula-option, you can add an Excel-formula into a cell for exports. Command accepts both direct cell references or it can be attached to the existing query column via ColumnReference.
The direct cell reference formula takes an Excel cell reference as a parameter and a formula to calculate. Optional columnstyle can be used.
select 'dbr.export.options', 'formula:A10','=A5*2', '# ##0';
With ColumnReference a calculation can be added to an existing result set.
/* For Excel output */ select 'dbr.export.options', 'formula:[Calculated]','=INDIRECT(ADDRESS(ROW(),COLUMN()-1))*2', '# ##0'; /* For HTML output */ select 'dbr.calc', 'Calculated', '[value]*2'; select ID, value, null as 'Calculated'; from mydata;
When you do large exports (>10,000 rows), it is faster to use CSV export than Excel. Excel document (which internally is zipped XML-files) is much more resource intensive.
If your report contains a simple export of a table, you can take advantage of the direct_mode
-option which will skip most of the myDBR code checks (like crosstabs etc), resulting with faster export.
The direct cell reference formula takes an Excel cell reference as a parameter and a formula to calculate. Optional columnstyle can be used.
select 'dbr.export.options', 'direct_mode',1;
Sample export times on a large export (50,000 rows with 12 columns):
Format | Time |
---|---|
Excel | 2m 51s |
Excel direct_mode | 2m 14s |
CSV | 35s |
CSV direct_mode | 11s |