Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    4
    Thanks
    0
    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
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 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
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    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:

    eg:
    Code:
    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.

    Code:
    window.open('data:application/pdf;charset=utf-8,pdf-source-code','_blank','height=300,width=400');
    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:
    http://apps.jgate.de/platform/source?jspdf

    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-17-2009 at 11:28 PM.
    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
    •