Giter Site home page Giter Site logo

tijspdf's Introduction

###Appcelerator Titanium - Image, Table and QRcode support in jsPDF###

This is a extendet version of JSPDF with support of WinANSI. You can write i.e. german umlaute Additional you can use tables and QR code inside PDF

Doku: https://mrrio.github.io/jsPDF/doc/symbols/jsPDF.html

TablePlugin: https://github.com/someatoms/jsPDF-AutoTable

Version 0.2

jsPDF libary

I do not claim to be the author of the jsPDF library, this code simply adds preliminary image support when used with Titanium.

This is a fork of the Titanium version of Malcom

addImage Method

doc.addImage(filename, format, x, y, w, h, imageWidth, imageHeight, imageSize);
/*
    filename
    format (always JPEG)
    x (on page)
    y (on page)
    width (on page)
    height (on page)

*/

addQRCode Method

doc.addQRCode({
    qr : {
        data : 'http://github.com/',
        ec : 'M'
    },
    x : 5,
    y : 100,
    width : 45
});

autoTable method

var columns = ["ID", "Name", "Country", "Count"];
var rows = [[1, "Shaw", "Tanzania", "12345"], [2, "Nelson", "Kazakhstan", "345567"], [3, "Garcia", "Madagascar", "8365734"]];
var options = { // Styling
    theme: 'striped', // 'striped', 'grid' or 'plain'
    styles: {},
    headerStyles: {},
    bodyStyles: {},
    alternateRowStyles: {},
    columnStyles: {},
    // Properties
    startY: false, // false indicates the margin.top value
    margin: 10,
    pageBreak: 'auto', // 'auto', 'avoid', 'always'
    tableWidth: 'auto', // number, 'auto', 'wrap'
};
doc.addAutoTable({
    headers:columns,
    data: rows,
    options : options
});

Example Code

module.exports = function() {
    var PDF = new (require('de.appwerft.jspdf'))();
    PDF.setProperties({
        title : 'Übertitel',
        subject : 'Betreff',
        author : 'John Doe',
        keywords : 'one, two, three',
        creator : 'Düzgün Jildiz'
    });
    PDF.addQRCode({
        qr : {
            data : 'Wer das liest ist doof.'
        },
        x : 160,
        y : 10,
        width : 40
    });
    PDF.setFont("Helevetica");
    PDF.addAutoTable({
        headers : ["ID", "Name", "Country", "Count"],
        data : [[1, "Noname", "Ödland", "12345"], [2, "Nelson", "Kazakhstan", "345567"], [3, "Garcia", "Madagascar", "8365734"]],
        options : {
            margin : {
            top : 70,
            left : 10,
        },
        tableWidth : '100%'
        }
    });
    //console.log(PDF.autoTableEndPosY());
/*
*/

    PDF.setDrawColor(0);
    PDF.addImage(Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,  'assets/image1.jpg').nativePath, 'JPEG', 100	, 180, 128, 72);
  	PDF.setFontType("bold");
    PDF.setFontSize(27);
    PDF.text( PDF.splitTextToSize('Welch fieser Katzentyp quält da süße Vögel bloß zum Jux?', 110),10, 190);
    PDF.addPage();
    PDF.rect(20, 120, 10, 10);
    // empty square
    PDF.rect(40, 120, 10, 10, 'F');
    // filled square
    PDF.addImage(Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'assets/image2.jpg').nativePath, 'JPEG', 70, 10, 100, 120);
    PDF.setFont("helvetica");
    PDF.setFontType("normal");
    PDF.setFontSize(24);
    var timeStampName = 'Angebot_' + Ti.App.Properties.getInt('nr', 0);
    var _tempFile = Ti.Filesystem.getFile(Ti.Filesystem.getTempDirectory(), timeStampName + '.pdf');
    PDF.save(_tempFile);
    return _tempFile;
};

tijspdf's People

Contributors

stgrosshh avatar appwerft avatar core-13 avatar raefa avatar

Stargazers

Martyn Joyce avatar Joseph avatar Steven avatar Nazır Doğan avatar  avatar Frank Eijking avatar Boydlee Pollentine avatar Konstantin Büschel avatar Marcel Pociot avatar

Watchers

 avatar James Cloos avatar Sebastian Klaus avatar  avatar Ramzi avatar

tijspdf's Issues

PDF with image added can't open in Adobe Acrobat - corrupted or damaged

On Android using Ti SDK 5.0.0.GA I am adding an image to a PDF. I am able to open the image using the Google PDF viewer but not Adobe Acrobat. I sent the message to my desktop and the same applies. I was able to view the image using "Preview" but not Adobe Acrobat. The error message says "The document cannot be opened because it is corrupted or damaged." If I remove the image from my PDF, the PDF will open in Acrobat. Odd right?

@AppWerft I tried different types of images and started looking into the jsPDF library but pretty soon got lost. Sorry I can't be of assistance fixing this. Hopefully I am helping a little finding these things! :)

Does not work if JPEG images have been optimised using ImageOptim

It took me a while to figure this out! Image Optim (https://imageoptim.com/) must strip out the headers and jsPDF must rely on the headers to get the dimensions of the file. Should probably fail a little more gracefully than this:

[ERROR] [iphone, 8.4, 192.168.0.116] {
  "stack": "getJpegSize\naddImage\nsubmitForm",
  "column": 73,
  "line": 2163,
  "message": "getJpegSize could not find the size of the image"
}

PDF wont open on non mobile devices

Hi,

When using addImage the image loads fine on the mobile device but once transferred to a PC or Mac the pdf reader reports the pdf is corrupt.

I tried to add a base64 image but looking at the code it doesn't support this. Is this a known bug or do you have to do something specific to get it to load. The pdf will load on any device regardless of if the image is on the device.

Thanks

Images embedded from the camera are rotated 90 degrees on iOS

If you add an image that you have saved from the camera and embed it in a PDF, it rotates 90 degrees on iOS (not Android).

var imageFile = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, "file-from-camera.png"); 
var tempFile = Ti.Filesystem.createTempFile(Ti.Filesystem.tempDirectory);
tempFile.write(ImageFactory.compress(imageFile.read(), 1));
pdf.addImage(tempFile.nativePath, 'JPEG', imageX, imageY, imageWidth, imageHeight);

As a workaround, I am adding the image to a view and using toImage() and creating a new image. It appears the right way up then!

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.