This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,805 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

Create a PDF file from HTML content

BlueForest85
BlueForest85 Member Posts: 129 Green Ribbon

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 !

Tagged:

Answers

  • BlueForest85
    BlueForest85 Member Posts: 129 Green Ribbon
  • Billy Verreynne
    Billy Verreynne Software Engineer Cape TownMember Posts: 28,976 Red Diamond

    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
    


  • InoL
    InoL Member Posts: 10,361 Blue Diamond

    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.