# JourneyPrinter

{% hint style="info" %}
**Version compatibility**

This feature was introduced in version **19.12.2** of the JourneyApps Container for iOS and Android, and version **20.4.1** for Desktop. Version **4.72.0** of the JourneyApps Runtime is also required.
{% endhint %}

JourneyApps can print a PDF directly using the native printing capabilities provided by the operating system.

### How to check if JourneyPrinter is supported

You can also check that printing is supported:

```javascript
    var capabilities = JourneyPrinter.getCapabilities();
```

**Returns**

```json
    {
        print: true|false,
        generatePdf: true|false
    }
```

{% hint style="info" %}
**Note**: For Runtime versions below 4.72.0, the `JourneyPrinter` object will not be available.
{% endhint %}

### Printing a PDF

To print a PDF, you need to provide the `JourneyPrinter.printPDF` function a PDF in the format of an ArrayBuffer or a base 64 string. In the case of an attachment object, helper functions are available:

```
    // Print a PDF from an ArrayBuffer:
    JourneyPrinter.printPdf(view.pdf.toArrayBuffer());
    
    // Print a PDF from base 64 string:
    JourneyPrinter.printPdf(view.pdf.toBase64());
```

The origin of the PDF data can also be another source, e.g. using an `<html/>` component to generate a PDF and send it in base 64 format to the app to be printed.

### Generate a PDF

{% hint style="info" %}
This feature was introduced in version **20.10.1** of the JourneyApps Container for Desktop. It is not available on mobile or web containers. Version **4.77.0** of the JourneyApps Runtime is also required.
{% endhint %}

#### Format

```javascript
JourneyPrinter.generatePdf(htmlString, options);
```

where `htmlString` is a string of HTML with `@page` or `@media print` CSS rules to control how the PDF is displayed. In addition, the `<head>` tag of the HTML must also contain this `<meta>` tag:

```html
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
```

and optional `options`:

| value           | Details                                                                                   | Default |
| --------------- | ----------------------------------------------------------------------------------------- | ------- |
| landscape       | Boolean to generate the PDF in landscape orientation                                      | false   |
| marginType      | Number to specify the type of margin. 0 - default margin, 1 - no margin, 2 minimum margin | 0       |
| printBackground | Boolean to choose to print the background                                                 | true    |
| pageSize        | Size of the page. 'A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid'                          | 'A4'    |

**Returns**: The generated PDF as an **ArrayBuffer**.

#### Example

```javascript
    var capabilities = JourneyPrinter.getCapabilities();
    if (capabilities.generatePdf == true) {
      // generate a PDF from an HTML string
      var generatedPdf = JourneyPrinter.generatePdf(htmlString, options);

      // create an Attachment object from the generated PDF ArrayBuffer:
      view.generatedPdf = Attachment.create({data: generatedPdf, mediaType: 'application/pdf'});

    }
```
