In oracle APEX, it is not easy to generate pdf file from a dymic content specially from APEX 24.2 or earlier version. There is no built in process to generate pdf file from dynamic content. That's why some times we need to depend on thirds party tools to accomplish our jobs.
I considered that you have already a dynamic content in your application's page. I try to resolve this critical business logic with the help of no third party tools, just only using 2 javascripts library. This process is discuss below step by step. For visual guidance, you follow the process from here (Html2Pdf).
STEP-1: Copy the jsPdf library file link from the Code Depository Network (CDN). Add the link to the page level javascripts file URLs.
STEP-2: Create javascript function to convert html document to PDF file. Click on the Function and Global Variable Declaration icon to open the code editor. Paste the below code here-
function html2pdf(htmlContentId, callback) {
let htmlContent = document.getElementById(htmlContentId);
const { jsPDF } = window.jspdf;
const doc = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4'
});
doc.html(htmlContent, {
x: 15,
y: 15,
width: 310,
windowWidth: 1094,
html2canvas: {
scale: 0.16,
useCORS: true,
logging: false
},
callback: function(doc){
const arrayBuffer = doc.output('arraybuffer');
callback(arrayBuffer);
}
})
}
function pdfUsage(htmlContentId, state) {
html2pdf(htmlContentId, function(arrayBuffer){
const blob = new Blob([arrayBuffer], {type:'application/pdf'});
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
if (state === 'Download') {
link.download = 'Application.pdf';
document.body.appendChild(link);
link.click();
} else if (state === 'Preview') {
link.download = 'Application.pdf';
window.open(url, '_blank');
} else if (state === 'Mail') {
apex.message.clearErrors();
const reader = new FileReader();
reader.onloadend = function() {
const base64data = reader.result;
const clob = base64data.split(',')[1];
apex.server.process(
'SENT_PDF_AS_ATTACHMENT'
, {
p_clob_01: clob
}
, {
dataType: 'text',
success: function() {
apex.message.showPageSuccess('Mail sent successfully with attachemnt');
}
}
)
}
reader.readAsDataURL(blob);
}
})
}
STEP-3: Create an AJAX Callback process to send the mail. Click on the "Process" icon from the left side navigation pane. Right click on the "Ajax Process" and select "Create Process". Set it name as "SENT_PDF_AS_ATTACHMENT" and open the "PL/SQL Code" editor. Paste the below code here-
DECLARE
l_file BLOB;
l_mail_id NUMBER;
BEGIN
l_file := apex_web_service.clobbase642blob(
p_clob => apex_application.g_clob_01
);
l_mail_id := APEX_MAIL.SEND(
p_to => 'rummanalam@yahoo.com',
p_from => 'rummancnsl@gmail.com',
p_body => 'This is a test mail for html 2 pdf converted report attachment. Please ignore it.',
p_subj => 'Html2pdf Report Converted & Sent as Attachment'
);
APEX_MAIL.ADD_ATTACHMENT(
p_mail_id => l_mail_id,
p_attachment => l_file,
p_filename => 'Html2pdf',
p_mime_type => 'application/pdf');
END;
Now Save your page and run. Hope, it will perfectly sent an email perfectly. Keep it mind that, this tutorial is for only showing to convertion of Html to Pdf nothing else. If you stuck on sending email then you need to configure your email first. You can find solutions to configure email from both Youtube and here.
Let's Enjoy! Happy coding.
No comments:
Post a Comment