# Printing and exporting the report output

How to print and export reports using the ActiveReportsJS API

## Content

### PageDocument class

As we mentioned in the [Executing a report](/activereportsjs/docs/v6.1/DeveloperGuide/api/report-executing) section, the result of invoking the `run` method of the `PageReport` instance is a [Promise object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) fulfilled with the instance of the [PageDocument](/activereportsjs/api/v6.1/classes/Core.PageDocument) class that allows you to print and export the report output.

### Printing the report output

The process of printing a report output is achieved by exporting the report to HTML and opening the result in the built-in Print Preview UI of a browser. The `print` method of the `PageDocument class` facilitates this process and offers the option to include three optional arguments:

* `renderOptions` – for printing, this can be set to `undefined`
* `onProgress` – a callback function that is invoked after each page of the report has been rendered to HTML. It should accept a single number argument that corresponds to the number of pages rendered.
* `checkCancel` – a callback function that is invoked after each page of the report has been rendered. It should return a boolean value indicating whether the printing process should be halted.

You can utilize these optional arguments, `onProgress` and `checkCancel`, to implement a custom user interface that displays the progress of the print job and allows for its cancellation in response to certain user events, such as clicking a `Cancel` button. The code below prints the report and sends information about the progress to the console.

```ts
import {Core} from "@mescius/activereportsjs";

const report = new Core.PageReport();
await report.load(`assets/Products.rdlx-json`);
const document = await report.run();
document.print(undefined, x=>console.log(`${x} pages ready`), ()=>{
    return false;
});
```

### Exporting the report output

`ActiveReportsJS API` allows you to export the report output to `PDF`, `HTML`, `CSV`, or `Excel` document providing the `PdfExport`, `HtmlExport`, and `TabularDataExport`, and `XlsxAdvExport` objects, respectively. All of them are part of the `Core library`.

```ts
  import {
    PdfExport,
    HtmlExport,
    TabularDataExport,
    XlsxAdvExport
  } from "@mescius/activereportsjs";
```

Each export object provides the `exportDocument` method with four arguments:

* `source` – the instance of the `PageDocument` class described above.
* `settings` – settings of the exported document. This argument is specific for each export type:
    * The `PDFExport.exportDocument` accepts the [PdfSettings object](/activereportsjs/api/v6.1/modules/PdfExport#pdfsettings)
    * The `HtmlExport.exportDocument` accepts the [HtmlSettings object](/activereportsjs/api/v6.1/modules/HtmlExport#htmlsettings)
    * The `TabularDataExport.exportDocument` accepts the [TabularDataSettings object](/activereportsjs/api/v6.1/modules/TabularDataExport#tabulardatasettings)
    * The `XlsxAdvExport.exportDocument` accepts the `XlsxAdvSettings` object.
* `onProgress` – a callback function that is invoked after each page of the report has been exported. It should accept a single number argument that corresponds to the number of pages exported.
* `checkCancel` – a callback function that that is invoked after each page of the report has been rendered. It should return a boolean value indicating whether the exporting process should be halted.

The `exportDocument` method returns a [Promise object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that is fulfilled with the result that is specific for each export type:

* The `PDFExport.exportDocument` fulfills the promise with the [PdfExportResult object](/activereportsjs/api/v6.1/modules/PdfExport#pdfexportresult) that allows downloading the result with the `download` method or store the result using the `data` property of [Blob type](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
* The `HtmlExport.exportDocument` fulfills the promise with the [HtmlExportResult object](/activereportsjs/api/v6.1/modules/HtmlExport#htmlexportresult) that allows downloading the result with the `download` method or store the result using the `data` property, which is either a `string` or `Blob`, depending on the value of the `multiPage` property of `HtmlSettings`.
* The `TabularDataExport.exportDocument` fulfills the promise with the [TabularDataExportResult object](/activereportsjs/api/v6.1/modules/TabularDataExport#tabulardataexportresult) that allows downloading the result with the `download` method or store the result using the `data` property, which is either a `string` or `Blob`, depending on the value of the `outputType` property of `TabularDataSettings.csvSettings`.
* The `XlsxAdvExport.exportDocument` fulfills the promise with the `XlsxExportResult object` that allows downloading the result with the `download` method or store the result using the `data` property of [Blob type](https://developer.mozilla.org/en-US/docs/Web/API/Blob).

Below are the code examples for each export type

```ts
  import {
    PdfExport,
    HtmlExport,
    TabularDataExport,
    XlsxAdvSettings
  } from "@mescius/activereportsjs";



async function exportPdf(document: PageDocument){
  const pdfSettings: PdfExport.PdfSettings = {
      info: {
          title: "Product List"
      }
  }
  const result = await PdfExport.exportDocument(document, pdfSettings);
  result.download("products.pdf");
}

async function exportHtml(document: PageDocument){
   const htmlSettings: HtmlExport.HtmlSettings = {
       title: "Product List",
       multiPage: false        
   }
   const result = await HtmlExport.exportDocument(document, htmlSettings);
   result.download("products.html");
}

async function exportCSV(document: PageDocument){
   const csvSettings: TabularDataExport.TabularDataSettings = {
       csvSettings: {
           colSeparator: ","
       }
   }
   const result = await TabularDataExport.exportDocument(document, csvSettings);
   result.download("products.csv");
}

async function exportExcel(document: PageDocument){
   const excelSettings: XlsxAdvSettings = {
       info: {
           creator: "ActiveReportsJS"
       },      
   }
   const result = await XlsxAdvExport.exportDocument(document, excelSettings);
   result.download("products.xlsx");
}


 async function onExport(){
    const report = new Core.PageReport();
    await report.load(reportUrl);
    var document = await report.run();
    exportHtml(document);
    exportCSV(document);
    exportPdf(document);
    exportExcel(document);
 }
```