Code Monkey home page Code Monkey logo

pdf.js's Introduction

pdf.js - create basic pdf files in the browser or node.js, simple as cake

### online demo @ http://maraksquires.com/pdf.js/ ## USAGE ### browser -
  <script src = "./lib/pdf.js" type = "text/javascript"></script>
  <script>

    /* create the PDF document */

      var doc = new pdf();
      doc.text(20, 20, 'hello, I am PDF.');
      doc.text(20, 30, 'i was created in the browser using javascript.');
      doc.text(20, 40, 'i can also be created from node.js');

      /* Optional - set properties on the document */
      doc.setProperties({
        title: 'A sample document created by pdf.js',
        subject: 'PDFs are kinda cool, i guess',    
        author: 'Marak Squires',
        keywords: 'pdf.js, javascript, Marak, Marak Squires',
        creator: 'pdf.js'
      });
      doc.addPage();

      doc.setFontSize(22);
      doc.text(20, 20, 'This is a title');

      doc.setFontSize(16);
      doc.text(20, 30, 'This is some normal sized text underneath.');

      doc.drawLine(100, 100, 100, 120, 1.0, 'dashed');
      doc.drawLine(100, 100, 120, 100, 1.2, 'dotted');
      doc.drawLine(120, 120, 100, 120, 1.4, 'dashed');
      doc.drawLine(120, 120, 120, 100, 1.6, 'solid');

      doc.drawRect(140, 140, 10, 10, 'solid');

      var fileName = "testFile"+new Date().getSeconds()+".pdf";
      var pdfAsDataURI = doc.output('datauri', {"fileName":fileName});

    /* inject the pdf into the browser */

      // inject using an iframe
      // this seems to work in FF but not Chrome? try testing some more on your own >.<
      //$('#theFrame').attr('src',pdfAsDataURI);

      // inject using an object tag
      // doesnt really work but it does something interesting
      //$('body').append('<object data="'+pdfAsDataURI+'" type="application/pdf"></object>');

      // inject changing document.location
      // doesn't work in FF, kinda works in Chrome. this method is a bit brutal as the user sees a huge URL
      // document.location = pdfAsDataURI;

      // create a link
      // this seems to always work, except clicking the link destroys my FF instantly 
      $('#pdfLink').html('<a href = "'+pdfAsDataURI+'">'+fileName+'</a> <span class = "helper">right click and save file as pdf</span');

  </script>

node.js -

  var sys = require('sys');
  var fs = require('fs');
  var pdf = require('./lib/pdf').pdf;

  /* create the PDF document */

  var doc = new pdf();
  doc.text(20, 20, 'hello, I am PDF.');
  doc.text(20, 30, 'i was created using node.js version: ' + process.version);
  doc.text(20, 40, 'i can also be created from the browser');

  /* optional - set properties on the document */
  doc.setProperties({
    title: 'A sample document created by pdf.js',
    subject: 'PDFs are kinda cool, i guess',    
    author: 'Marak Squires',
    keywords: 'pdf.js, javascript, Marak, Marak Squires',
    creator: 'pdf.js'
  });
  doc.addPage();

  doc.setFontSize(22);
  doc.text(20, 20, 'This is a title');

  doc.setFontSize(16);
  doc.text(20, 30, 'This is some normal sized text underneath.');

  doc.drawLine(100, 100, 100, 120, 1.0, 'dashed');
  doc.drawLine(100, 100, 120, 100, 1.2, 'dotted');
  doc.drawLine(120, 120, 100, 120, 1.4, 'dashed');
  doc.drawLine(120, 120, 120, 100, 1.6, 'solid');

  doc.drawRect(140, 140, 10, 10, 'solid');

  var fileName = "testFile"+new Date().getSeconds()+".pdf";

  fs.writeFile(fileName, doc.output(), function(err, data){
    sys.puts(fileName +' was created! great success!');
  });

Authors

Marak Squires and Matthew Bergman

Heavily inspired by James Hall's jsPDF

pdf.js's People

Contributors

marak avatar fotoverite avatar mklabs avatar izuzak avatar

Watchers

Rai Escarez avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.