Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Create a PDF file from HTML content

Hello everyone,
I'm wondering how I could generate a PDF file from HTML content.
I don't know if it's natively possible, but I took a peek on the plug-in page :
This one seems interesting, with its demo : https://apex.world/ords/f?p=100:710:7521831718395::::P710_PLG_ID:AT.RAMMELHOF.CLIENT-SIDE-PDF
However I want to know your point of view about this and how to proceed.
Thank you for your experience !
Answers
-
Anyone ?
-
I use Chrome in headless mode as a Perl-based CGI web service.
You call it with a URL as parameter and Perl runs Chrome's print-to-pdf feature on that URL passed.
Basic Perl CGI script below. I wrote back in 2019. There were Chrome bugs with ignoring certs. And I do not know Perl.
For APEX URLs you need to work around authentication and APEX item values for the page. I passed a "proxy URL" to the Perl CGI with a unique token parameter for the public APEX proxy page URL. This token was used to perform a transparent logon and redirect to the APEX page (with values), for Chrome to render (in headless mode) and save to PDF.
#!/usr/bin/perl # # bug filed for https acceptInsecureCerts # https://bugs.chromium.org/p/chromium/issues/detail?id=721739 # use CGI; $myCgi = new CGI; $base = $myCgi->param('base'); $pdfFile = $myCgi->param('filename'); $download = $myCgi->param('auto-download'); if (($base eq "") || ($base eq "null")) { print "Expires: 0\n"; print "Pragma: no-cache\n"; print "Content-Type: text/plain;\n\n"; print "Parameter 'base' is required, but not detected.\nYour URL call cannot be serviced.\n"; print "\n\n"; print "usage: base=<url>[&filename=<file.pdf>][&auto-download=yes]\n"; exit } $URL=$base; $direct = "/tmp/"; $myFile = "$direct".time()."_$$-1.pdf"; # `google-chrome runs as --headless --disable-gpu --use-gl --virtual-time-budget=10000 --ignore-certificate-errors --print-to-pdf="$myFile" "$URL" 1>/dev/null 2>/dev/null`; # Have to run as chrome user in order for /home/chrome/.config to be used, containing wallet and certificates - thus sudo as chrome is used `sudo -u chrome /home/chrome/bin/google-chrome "$myFile" "$URL" `; print "X-cgi-name: PageToPDF.cgi - $$\n"; print "X-pdf-url: $URL\n"; print "Expires: 0\n"; print "Pragma: no-cache\n"; if ($pdfFile ne "") { if ($download ne "") { print "Content-Disposition: attachment; filename=\"$pdfFile\"\n"; } else { print "Content-Disposition: filename=\"$pdfFile\"\n"; } } print "Content-Type: application/pdf;\n\n"; open (FH, $myFile); while (my $line = <FH>) { print ("$line"); } close(FH); unlink( $myFile ); #eof
-
I've used jsPDF for this. It lets you create a pdf of the whole page, or just a specific region if needed. After attaching the JavaScript libraries jsPDF and html2canvas, you can use this as a starting point (e.g. using a Print button):
window.jsPDF = window.jspdf.jsPDF; var doc = new jsPDF(); doc.html(document.getElementById('container'), { callback: function (doc) { doc.save(); }, width: 170, windowWidth:700, margin: [20, 0, 0, 20] });
where 'container' is the static ID of the region you want to save as a pdf.
Saving the whole page as a pdf is easy, because you can use the native browser print option.