Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Pdf generator

  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Thanked 0 Times in 0 Posts

    Pdf generator

    Hi, is it possible to generate pdf file using javascript?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    No. JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database or the client's operating system, or alter the default behaviour of the browser.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Thanked 602 Times in 582 Posts
    Quote Originally Posted by ira19 View Post
    Hi, is it possible to generate pdf file using javascript?

    yes it is, in some browsers.

    You can use XSL FO to create PDFs from HTML/XML data.

    it would be available to all users if you did it on the server though...
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #4
    New Coder
    Join Date
    May 2006
    Thanked 0 Times in 0 Posts
    You can definitely generate PDF with client side JavaScript.

    Displaying it as a PDF file to the user may be an issue.

    Because PDF is just a file format, if you know how to generate this file format, you can do it with JavaScript.

    But because it is a file format separate from HTML, the browser views it is through a URL, or as an embedded object.

    Taking the PDF source you've generated and passing it to the embed, or linking it to a URL is what you have to figure out.

    You cannot do this via sending the browser to a URL:

    window.loaction = 'mypdf_file_url';
    mainly because you cannot generate that URL from the client side.

    You can however use a data URI: http://en.wikipedia.org/wiki/Data_URI_scheme
    Write this to a PDF embed with the PDF source you generated in JavaScript. Most browsers now support it.

    You could use window.open() or window.write() to write the data to the window.

    You can also embed it in a url, so it is like a normal link. But you generated the actual pdf data with JavaScript.

    I'm sure you can find other ways of doing it.

    Here is an example of PDF generation in pure JavaScript:

    Though this is server side, it will work on the client side with modifications. On the server side, it is easy to display the PDF, or intiate a download, since you can do it through the HTTP Specifications.

    On the client side, you cannot initiate a HTTP request to yourself, so you'll have to use other protocols described above.
    Last edited by digital-ether; 06-18-2009 at 12:28 AM.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts