jaw
jaw

Reputation: 319

How i can display pdf file into region oracle apex?

i want display PDF file into region , i tried that by call application process using below code but always same file open.( plsql dynamic content region)

 DECLARE
 V_URL   VARCHAR2(2500);
 BEGIN
 V_URL :='f?p=&APP_ID.:1:&APP_SESSION.:APPLICATION_PROCESS=display_emp_blob:::FILE_ID:' ||:P6_ID;

 Sys.htp.p('<p align="center">');
 sys.htp.p('<iframe src="'||V_URL||'"width="99%" height="1000">');
 sys.htp.p('</iframe>');
 sys.htp.p('</p>');

END;

and the application process code in below

CREATE OR REPLACE PROCEDURE OPEN_FILE (P_ID NUMBER)
IS
 vBlob blob;
 vmimetype varchar2(50);
BEGIN
SELECT ORG_FILES.FILE_CONTENT,MIME_TYPE INTO vBlob, vmimetype
            FROM ORG_FILES
            WHERE ID =P_ID ;

 sys.HTP.init;
 owa_util.mime_header(vmimetype,false);
 htp.p('Content-Length: ' || dbms_lob.getlength(vBlob));
 owa_util.http_header_close;
 wpg_docload.download_file(vBlob);
 apex_application.stop_apex_engine;
   exception
    when no_data_found then
    null;
   END;

How i can open different PDF file into region based a value in ITEM (P6_ID) .

Upvotes: 0

Views: 6558

Answers (2)

Scott
Scott

Reputation: 5055

My apologies, on the rare occasion I use this region type, I always think it can be refreshed.

https://spendolini.blogspot.com/2015/11/refreshing-plsql-regions-in-apex.html

The solution is to create a classic report that calls a PL/SQL function that returns your HTML.

SELECT package_name.function_name(p_item => :P1_ITEM) result FROM dual

Upvotes: 0

Edgar Streuli
Edgar Streuli

Reputation: 248

I think the problem you have is that the browser caches the file.

You can specify the time the browser caches with the "Cache-control" header option. Below, you have the code that I use (I have this code in the application process, not in the database):

sys.htp.init;
sys.owa_util.mime_header( 'application/pdf', FALSE );
sys.htp.p('Content-length: ' || sys.dbms_lob.getlength( v_blob));
sys.htp.p('Content-Disposition: inline; filename="'|| v_filename || '"' ); -- "attachment" for download, "inline" for display
sys.htp.p('Cache-Control: max-age=3600');  -- in seconds. Tell the browser to cache for one hour, adjust as necessary
sys.owa_util.http_header_close;
sys.wpg_docload.download_file( v_blob );

apex_application.stop_apex_engine;

You can also try some lazy load, which is the way I access my files (It may be that the way to access your file is also part of the problem). This way you make the page load without waiting for the user and then it loads and shows the file. I don't use the iframe tag but the embed tag. The way to do it is as follows:

  • Create a region with static content with this html
<div id="view_pdf"></div>
  • creates a dynamic action when the page loads, which executes javascript and add the following code
$('#view_pdf').html('');
var url = 'f?p=&APP_ID.:1:&APP_SESSION.:APPLICATION_PROCESS=display_emp_blob:::FILE_ID:'  + apex.item('P6_ID').getValue();
var preview = document.createElement('embed'); 
preview.type = "application/pdf";
preview.width="100%";
preview.height="625px";
preview.src = url;
$("#view_pdf").append(preview);

You can modify the values depending on what you need. The embed tag uses the default way to view pdf files from browsers.

Also if what you want is to change the pdf without reloading the page you must use the previous javacript in a dynamic action when you change the value of the item.

I hope you find it useful.

Upvotes: 3

Related Questions