Forum Stats

  • 3,872,122 Users
  • 2,266,386 Discussions
  • 7,911,052 Comments

Discussions

How to Download HTML as PDF that is generated through a Function (PL/SQL) in Oracle APEX?

Adnan3114
Adnan3114 Member Posts: 13 Red Ribbon
edited Aug 3, 2022 12:41PM in APEX Discussions

Hello

As the question states, is there any way to manually download HTML content that is generated from a function to PDF?

I have already succeeded downloading a div as PDF using "html2pdf.js" javascript library which works fine on a PL/SQL content body Region type. But I want to create a classic report that will have the option to download the desired HTML report on a single column like this:

"SELECT primary_key, col1, col2, FUNCTION_RETURNING_HTML(primary_key) "DOWNLOAD" from TABLE"

Without displaying the HTML content on the report.

Is there any way to do this or any alternatives? (we can't use any reporting tool)

EDIT: I have found a solution and pinned it as Answer below.

Ashraf86

Best Answer

  • Adnan3114
    Adnan3114 Member Posts: 13 Red Ribbon
    edited Aug 3, 2022 12:51PM Answer ✓

    I have managed to find a solution.

    Turns out changing Region header type to hidden and setting the WHOLE REPORT on the region header seems to work and downloads the report in PDF.

    Here are the steps:

    1. In the APEX page I have created a region (name: reg_1) which has these attributes: Header - Hidden, Style - Remove UI Decoration, Remove Body Padding. This region will only serve for us to download the PDF and nothing else.
    2. Now in the reg_1 region's header, I have added this HTML: <div id="content"></div>
    3. Created a page item :PX_PK_ID.
    4. Then, created a Dynamic action change event for :PX_PK_ID that has 2 true actions. This dynamic action's scope should be set to Dynamic.
    5. Action 1- SQL Statement Set Value: select FNC_RETURN_HTML(:PX_PK_ID) from dual; and for jQuery Selector #content (ID of the header HTML in step-2).
    6. Action 2- Execute Javascript Function that is down_pdf() which will download the elements in #content div using html2pdf.js library after Action 1 is complete.
    7. Finally, create the classic report region: SELECT PRIMARY_KEY, col1, col2, "DOWNLOAD" as DOWNLOAD from TABLE and for the DOWNLOAD column, change its type to link and set the link to URL like this: javascript:$s("PX_PK_ID",#PRIMARY_KEY#);
    8. Voila! It works like a charm!
    Ashraf86

Answers

  • InoL
    InoL Member Posts: 10,179 Blue Diamond

    Without displaying the HTML content on the report.

    If it is a hidden column, the output is not available for your JavaScript function.


    Depending on your database version, you can run the same JavaScript in the database:

    https://blogs.oracle.com/java/post/multilingual-engine-executing-javascript-in-oracle%C2%A0database

    Maybe you can use html2pdf directly in the database to convert the output of your FUNCTION_RETURNING_HTML.


    Another option is open source pdf packages, e.g.:

    Instead of FUNCTION_RETURNING_HTML, create a function FUNCTION_RETURNING_PDF.

    Adnan3114
  • Adnan3114
    Adnan3114 Member Posts: 13 Red Ribbon

    @InoL Thanks for the suggestions.

    1. Our live software is running on 11g database and APEX 5.1, but we are migrating it to 19.0c database with APEX 22.1. So we can't actually run the Javascript code from database.
    2. We already have set up the reports as function that returns HTML so it would be really time consuming (which we can't afford at this moment) to convert those to be able to use PL/SQL PDF gen feature.

    I am trying to download the hidden div but I am unable to make it work. I even tried to push the div out of displayable regions using CSS but that too was no use.

  • Adnan3114
    Adnan3114 Member Posts: 13 Red Ribbon
    edited Aug 3, 2022 12:51PM Answer ✓

    I have managed to find a solution.

    Turns out changing Region header type to hidden and setting the WHOLE REPORT on the region header seems to work and downloads the report in PDF.

    Here are the steps:

    1. In the APEX page I have created a region (name: reg_1) which has these attributes: Header - Hidden, Style - Remove UI Decoration, Remove Body Padding. This region will only serve for us to download the PDF and nothing else.
    2. Now in the reg_1 region's header, I have added this HTML: <div id="content"></div>
    3. Created a page item :PX_PK_ID.
    4. Then, created a Dynamic action change event for :PX_PK_ID that has 2 true actions. This dynamic action's scope should be set to Dynamic.
    5. Action 1- SQL Statement Set Value: select FNC_RETURN_HTML(:PX_PK_ID) from dual; and for jQuery Selector #content (ID of the header HTML in step-2).
    6. Action 2- Execute Javascript Function that is down_pdf() which will download the elements in #content div using html2pdf.js library after Action 1 is complete.
    7. Finally, create the classic report region: SELECT PRIMARY_KEY, col1, col2, "DOWNLOAD" as DOWNLOAD from TABLE and for the DOWNLOAD column, change its type to link and set the link to URL like this: javascript:$s("PX_PK_ID",#PRIMARY_KEY#);
    8. Voila! It works like a charm!
    Ashraf86