EzDevInfo.com

pdfmake

Client/server side PDF printing in pure JavaScript

pdfmake - how to make very long image scroll down to second page?

I am using html2canvas to create dataURL of a html table. This table is quite long. PDFmake take the image, scales it, and tries to make the image fit into a single page. That make the image very, very tiny. Even an ant can't read the contents of the image of the table. I would like my image to scroll to as much pages as it would like to in the created pdf file. Can this be done?


Source: (StackOverflow)

Create pdf with non-ASCII characters

When I write Hebrew text in PDFMake it becomes '????' in the output.
Is there a way to create a PDF containing non-ASCII characters?


Source: (StackOverflow)

Advertisements

Lines, circles and other shapes?

Is it possible to draw lines, circles and other shapes with pdfmake? If yes, is there a documentation or are there any samples? I would like to replace jsPDF with pdfmake.


Source: (StackOverflow)

Cannot generate PDF file from Local Storage Ionic

I have problem when make an ionic app for IOS about cannot generate PDF file after get data from $localStorage using ngStorage plugin and PDFmake plugin . Its my controllers.js script below:

angular.module('starter.controllers', [])

.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
  // Form data for the login modal
  $scope.loginData = {};

  // Create the login modal that we will use later
  $ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });

  // Triggered in the login modal to close it
  $scope.closeLogin = function() {
    $scope.modal.hide();
  };

  // Open the login modal
  $scope.login = function() {
    $scope.modal.show();
  };

  // Perform the login action when the user submits the login form
  $scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);

    // Simulate a login delay. Remove this and replace with your login
    // code if using a login system
    $timeout(function() {
      $scope.closeLogin();
    }, 1000);
  };
})

.controller('PlaylistsCtrl', ['$scope', '$location', '$localStorage', function($scope, $location, $localStorage) {
  $scope.model = {
   phone: '',
   total: '',  
   type: '',
   discount: '',
    };

 $scope.producttype = [{
      product: "A",
      value: 7900000,
      discount1: 5612,
      discount2: 79000
    }, {
      product: "B",
      value: 10200000,
      discount1: 5612,
      discount2: 79000
    }, {
      product: "C",
      value: 11000000,
      discount1: 5612,
      discount2: 79000
    }, {
      product: "D",
      value: 7300000,
      discount1: 5612,
      discount2: 79000
    }, {
      product: "E",
      value: 10000000,
      discount1: 5612,
      potongan2: 79000
    }, {
      product: "F",
      value: 11200000,
      discount1: 5612,
      discount2: 79000
    }, ];




  $scope.create = function(model) {
    $scope.model.total = $scope.model.phone + 3; 
       $localStorage.model = $scope.model;
     console.log($localStorage.model);
$location.path('/app/more');
    }



}])

.controller('MoreCtrl', ['$scope', '$ionicHistory', '$localStorage', function($scope, $ionicHistory, $localStorage) {
      $scope.model = $localStorage.model;
   if ($scope.model.type == "Good") {
      $scope.model.selection = $scope.model.type.discount1;

      console.log($scope.model.discount);
    } else {
      $scope.model.selection = $scope.model.type.discount2;
      console.log($scope.model.discount);
    }


   var binaryArray = null;
var currentfileEntry = null;

$scope.docDefinition = {

  content: [
        { 
            text: 'Total Number', 
            style: 'header' 
        },
    { 
            text:  $scope.model.phone, 
            style: 'header' 
        },

    { 
            text:  $scope.model.selection, 
            style: 'header' 
        },



  ],
styles: {
        header: {
            fontSize: 18,
            bold: true,
      alignment: 'center'
        },
        subheader: {
            fontSize: 15,
            bold: true
        },
    table: {
            margin: [0, 5, 0, 15]
        },
        quote: {
            italics: true
        },
        small: {
            fontSize: 8
        }
    }  };


function fail(error) {
    console.log(error.code);
};

function gotFS(fileSystem) {
    var fileName = "your-file.pdf";
    fileSystem.root.getFile(fileName, {
      create: true,
      exclusive: false
    }, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    currentfileEntry = fileEntry;
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwrite = function (evt) {
      $scope.downloading = false;
      alert("The file was written successfully");
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSforRead, fail);
    }
    writer.write(binaryArray);
}
  $scope.createpdf = function(model) {
      $localStorage.model = $scope.model;
console.log ($scope.docDefinition);


       if (!window.cordova) {
      pdfMake.createPdf($scope.docDefinition).open();
    } else {
      pdfMake.createPdf($scope.docDefinition).getBuffer(function (buffer) {
        var utf8 = new Uint8Array(buffer); // Convert to UTF-8...                
        binaryArray = utf8.buffer; // Convert to Binary...
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
      });
    }
    }

}]);

I've got an error message on console log like "undefined" for $scope.model.selection after trying generate to a PDF file but can show on list. For more scripts for this problem you can download my example code on my repo.

How I can fix it?


Source: (StackOverflow)

pdfmake: How to create multiple page pdf with different orientation?

is there a way to create multiple page pdf with different page orientation using pdfmake?

to make it simple, I want something like this :

  • Page 1 with portrait orientation
  • page 2 with landscape orientation
  • page 3 with portrait orientation

I've tried it so many times with different method but it always effect to all page.


sorry for my terible english


Source: (StackOverflow)

How to save my generated javascript pdf file to my server's filesystem?

I'm using pdfmake to create my pdf and while it allows the user to open the pdf directly or download it to their computer, I'm not sure how I would go about generating the pdf and saving it to my server's file system.

From what I understand, there are plenty of security measures not allowing javascript to save data to file(s), so would sending it to my php backend be the only choice ? and how would i go about doing that ?

Thanks !


Source: (StackOverflow)

pdfmake - Get PDF like jsPDF function output()

I am using pdfmake to generate a PDF client side. It's working great on Chrome: I get a base64 encoded URL that opens in a new tab.

However, on my Cordova app on android, it does not work so I want to write the PDF to the device. When I use jsPDF doc.output(), I manage to write the pdf to file in this format:

%PDF-1.3
3 0 obj
<</Type /Page... etc

Does anyone know how to get this PDF code with pdfmake?


Source: (StackOverflow)

How can I remove some columns in pdf export in angular js ui Grid

I am using Angular JS ui Grid

http://ui-grid.info/docs/#/tutorial/312_exporting_data_complex

My requirement is that I want to show e.g. 5 columns, but when I export PDF, I don't want to export certain columns like username.

How can I do that?


Source: (StackOverflow)

ui-grid: How do I set column width when using pdf export

How can I set column widths when exporting pdf using ui-grid. Pdfmake has width as an option however the ui-grid exporter api docs don't have this option anywhere.

Thanks for the help


Source: (StackOverflow)

PDFmake - html table to pdfmake table?

I am trying to make an application, which contains html tables.

I want a download button, and onClick event, downloads a PDF file containing all the html tables in it.

Can it be done?

Can someone help me with a function that converts the html table into an object so that it can be pushed into a PDF file through pdfmake?


Source: (StackOverflow)

running a "background task" in javascript

Is it possible, in Javascript, to run a function in background ?

I am generating a pdf with pdfmake tool in an angularJS app, but the pdf generation is quite long (3-4 seconds) and during this time, the ui freeze completely.

I would like to run a background task and force the pdf download without freezing the user ui, is it possible ?

Here how I am running pdfmake (pdfmake and _ are custom factories):

'use strict';

angular.module('App')

    .service('CatalogPdfService', ['pdfmake', '_', '$q', '$filter',
        function (pdfmake, _, $q, $filter) {

            var $translate = $filter('translate');
            var listDate = new Date();

            return {
                download: download
            };

            function download(data) {

                listDate = _.first(data).publishedOn;
                console.log('start download');
                var deferred = $q.defer();
                var filename = $translate('APP.EXPORT.pdf.catalog.title', {date: $filter('amDateFormat')(listDate, 'DDMMYYYY')}) + '.pdf';
                create(data).download(filename, function () {
                    console.log('end download');
                    deferred.resolve();
                });
                return deferred.promise;
            }

            function create(data) {

                // group data by category
                var dataByCategory = _.groupBy(data, function (d) {
                    return d.category;
                });

                // group categories data by subcategory
                _.forEach(dataByCategory, function (d, i) {
                    dataByCategory[i] = _.groupBy(d, function (d) {
                        return d.subcategory;
                    });
                });

                var content = {
                    table: {
                        headerRows: 1,
                        widths: ['*', 20, 10, 20, 20, 20, 20, 40, 20, 30],
                        body: [
                            [
                                {text: $translate('APP.EXPORT.pdf.catalog.header.article')      , style: 'headings', alignment: 'left'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.mine')         , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.rank')         , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.origin')       , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.transporter')  , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.culture')      , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.label')        , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.unit')         , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.packing')      , style: 'headings'},
                                {text: $translate('APP.EXPORT.pdf.catalog.header.price')        , style: 'headings'}
                            ]
                        ]
                    },
                    layout: {
                        hLineWidth: function (i) {
                            return (i == 0) ? 0 : 1;
                        },
                        vLineWidth: function (i) {
                            return 0;
                        },
                        hLineColor: function (i, node) {
                            return '#ccc';
                        }
                    }
                };

                _.forEach(dataByCategory, function (data, category) {
                    content.table.body = content.table.body.concat(renderCategory(category, data));
                });

                var dd = {};
                dd.content = renderHeader().concat(content);
                dd.header = function (currentPage, pageCount) {
                    return {
                        text: $translate('APP.EXPORT.pdf.catalog.pagecount', {start: currentPage.toString(), end: pageCount.toString()}),
                        alignment: 'right',
                        color: '#666',
                        margin: [0, 20, 40, 0]
                    };
                };
                dd.styles = {
                    title: {
                        fontSize: 15,
                        bold: true
                    },
                    headings: {
                        italics: true,
                        alignment: 'center'
                    },
                    flag: {
                        alignment: 'center',
                        italics: true,
                        color: '#666'
                    },
                    category: {
                        bold: true,
                        fontSize: 12,
                        margin: [0, 10, 0, 0] // Left, Top, Right, Bottom
                    },
                    subcategory: {
                        bold: true,
                        fontSize: 10,
                        margin: [0, 7, 0, 5] // Left, Top, Right, Bottom
                    }
                };

                dd.defaultStyle = {
                    fontSize: 8
                };

                return pdfmake.createPdf(dd);
            }

            function renderHeader() {
                return [
                    {image: logo(), height:40, width: 86},
                    {
                        margin: [0, 10, 0, 20],
                        table: {
                            widths: [100, 100, 100, '*'],
                            body: [
                                [
                                    {text: $translate('APP.COMMON.address', {char: '\n'})},
                                    {text: '\n' + $translate('APP.COMMON.phone')},
                                    {text: '\n' + $translate('APP.COMMON.fax')},
                                    {text: '\n' + $translate('APP.EXPORT.pdf.catalog.listno', {date: $filter('amDateFormat')(listDate, 'DD/MM/YYYY')}) , alignment: 'right'}
                                ]
                            ]
                        },
                        layout: {
                            hLineWidth: function (i) {
                                return (i == 0) ? 0 : 1;
                            },
                            vLineWidth: function (i) {
                                return 0;
                            }
                        }
                    }];
            }

            function renderCategory(name, data) {

                var category = [
                    [
                        {text: name, style: 'category', colspan: 10},
                        '', '', '', '', '', '', '', '', ''
                    ]
                ];

                _.forEach(data, function (data, name) {
                    category = category.concat(renderSubcategory(name, data));
                });

                return category;
            }

            function renderSubcategory(name, data) {

                var subcategory = [
                    [
                        {text: name, style: 'subcategory', colspan: 10},
                        '', '', '', '', '', '', '', '', ''
                    ]
                ];

                _.forEach(data, function (product) {
                    subcategory.push(renderProduct(product));
                });

                return subcategory;
            }

            function renderProduct(product) {
                return [
                    product.name,
                    {
                        text: (product.isInPrivateList ? 'Oui' : ''),
                        style: 'flag'
                    },
                    {
                        text: (null === product.rank ? '' : String(product.rank)),
                        style: 'flag'
                    },
                    {
                        text: (product.origin || ''),
                        style: 'flag'
                    },
                    {
                        text: (product.transporter || ''),
                        style: 'flag'
                    },
                    {
                        text: (product.label || ''),
                        style: 'flag'
                    },
                    {
                        text: (product.culture || ''),
                        style: 'flag'
                    },
                    {
                        text: product.unit,
                        margin: [0, 0, 5, 0],
                        italics: true,
                        alignment: 'right'
                    },
                    {
                        text: (product.quantity || '1'),
                        italics: true,
                        fillColor: '#eee',
                        alignment: 'center'
                    },
                    {
                        text: product.unitPrice,
                        margin: [0, 0, 5, 0],
                        italics: true,
                        fillColor: '#eee',
                        alignment: 'right'
                    }
                ];
            }

            function logo() {
                return 'data:image/jpeg;base64,blabla bigbase64 string'
            }
        }]);

Source: (StackOverflow)

Generating PDF file with Ionic framework

Is there any plugin for Ionic framework to generate a pdf file using html content?

Basically I need to create a html with values passed from an Ionic mobile application and some css styles, and then convert it into a pdf file which can be saved inside the local file system in a device (Android device and iOS device). I want to do this with a javascript like library so that it can be used in both Android and iOS devices.


Source: (StackOverflow)

Making pdfmake to work with large data

Pdfmake works fine when generating the report on my local machine. But it crashes when it gets generated on the actual browser(Only the table with huge data) . Is there a solution to this? Should I make my pdfmake code less complex?


Source: (StackOverflow)

Watermark using pdfmake

In Javascript, I am using pdfmake to generate a pdf document. I read it from github that it supports watermark and the following is my usage but it is giving me some random and weird characters. Those characters are the same no matter what texts are provided in watermark. Anyone got an idea?

pdfmake

enter image description here

enter image description here


Source: (StackOverflow)

Add border arround a paragraph with pdfmake

I'm generating pdf via pdfmake.

Let's say i have content of the pdf like this

var docDefinition = {
    content: [
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a pharetra odio.',
        'Vestibulum erat mauris, sodales et consequat sit amet, ultricies vitae erat. Etiam feugiat orci justo, ultrices malesuada dui ornare ac.',
    ]
};

Is it possible to add border arround one of the paragraphs or do I have to use tables for this?


Source: (StackOverflow)