FANTASTIC PDF VIEWER BUSINESS CENTRAL

PDFObject is a library that utilizes the standard built-in PDF renderer in a web browser, which can result in different rendering across different browsers.

This option is particularly useful for displaying PDFs in Business Central, as browser-based PDF viewers often come with powerful features.

I decided to showcase how to use PDFObject, a library that offers an embeddable mechanism and contains all the necessary checks to see if the user’s browser supports it.

There are pros and cons to using the built-in PDF renderer in a web browser. The main drawback is that we lose the ability to programmatically control the document, such as changing the way the browser renders or flipping pages.

However, we can be confident that the PDF is rendered as accurately as possible, and most modern browsers have good PDF renderers with all the necessary features.

To declare the ControlAddIn with the PDFObject library and our custom JS script with CSS, we can use the following code:

controladdin "PDFV2 PDF Viewer MF"
{
    Scripts = 'https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js', 'https://unpkg.com/pdfobject@2.2.8/pdfobject.min.js', 'PDFViewer/script.js';
    StartupScript = 'PDFViewer/Startup.js';
    StyleSheets = 'PDFViewer/stylesheet.css';

    MinimumHeight = 400;
    MinimumWidth = 100;
    MaximumHeight = 2000;
    MaximumWidth = 4000;
    HorizontalStretch = true;
    VerticalStretch = true;
    VerticalShrink = true;
    HorizontalShrink = true;

    event ControlAddinReady();
    procedure LoadPDF(PDFDocument: Text; IsFactbox: Boolean)
    procedure SetVisible(IsVisible: Boolean)
}

After initializing the control and setting its style, we can use the following code to load the PDF:

function InitializeControl(controlId) {
    var controlAddIn = document.getElementById(controlId);
    controlAddIn.innerHTML ='<div id="my-pdf"></div>';
}

function LoadPDFMF(PDFDocument,IsFactbox){
    var iframe = window.frameElement;

    requestAnimationFrame(() => {

        PDFObject.embed(PDFDocument, "#my-pdf");

        iframe.style.maxHeight = 1100 + 'px';
        iframe.style.height =  1100 + 'px';
    });
}

Finally, to view PDFs in Business Central, we can use a page that takes an InStream and uploads the PDF as Base64. Here is an example of the page:

page 99999 "PDFV2 PDF Viewer"
{
    Caption = 'MF PDF Viewer';
    PageType = Card;
    UsageCategory = None;
    layout
    {
        area(content)
        {
            group(General)
            {
                ShowCaption = false;
                usercontrol(PDFViewer; "PDFV2 PDF Viewer MF")
                {
                    ApplicationArea = All;

                    trigger ControlAddinReady()
                    begin
                        CurrPage.PDFViewer.LoadPDFMF(PDFAsTxt, false);
                    end;
                }
            }
        }
    }
    procedure SetPDFDocument(PDFInStream: InStream)
    var
        Base64Convert: Codeunit "Base64 Convert";
    begin
        PDFAsTxt := PDFAliasLbl + Base64Convert.ToBase64(PDFInStream);
   

Lascia un commento

Il tuo indirizzo email non sarĂ  pubblicato. I campi obbligatori sono contrassegnati *