<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sergey Kornilov</title>
    <description>The latest articles on Forem by Sergey Kornilov (@phprunner).</description>
    <link>https://forem.com/phprunner</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F966403%2Fc332b779-01ad-4834-8e9f-f2fcbc7d1d37.png</url>
      <title>Forem: Sergey Kornilov</title>
      <link>https://forem.com/phprunner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/phprunner"/>
    <language>en</language>
    <item>
      <title>Generate PDF files using NodeJS and PDFMake</title>
      <dc:creator>Sergey Kornilov</dc:creator>
      <pubDate>Tue, 08 Nov 2022 23:43:51 +0000</pubDate>
      <link>https://forem.com/phprunner/generate-pdf-files-using-nodejs-and-pdfmake-2gie</link>
      <guid>https://forem.com/phprunner/generate-pdf-files-using-nodejs-and-pdfmake-2gie</guid>
      <description>&lt;p&gt;In this article we will show you how to generate PDF invoices on the web server side. At &lt;a href="https://xlinesoft.com/"&gt;Xlinesoft&lt;/a&gt;, we use this approach to generate and email billing reminders to our customers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nodejs.org/en/download/"&gt;Download&lt;/a&gt; NodeJS to your web server. For Windows choose 64-bit MSI installer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run and install keeping all default settings. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside your project create a new folder named &lt;strong&gt;pdfmake&lt;/strong&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Proceed to that folder, start the command line and run:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install pdfmake
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;This will create index.js, in this file we will add the code that create PDF file. In our case, we will be generating due invoices for our customers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://pdfmake.github.io/docs/0.1/getting-started/server-side/"&gt;PDFMake documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the &lt;strong&gt;index.js&lt;/strong&gt; file we that generates invoices.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var fonts = {
   Roboto: {
     normal: 'jsnode/fonts/DejaVuSans.ttf',
     bold: 'jsnode/fonts/DejaVuSans-Bold.ttf',
     italics: 'jsnode/fonts/DejaVuSans.ttf',
     bolditalics: 'jsnode/fonts/DejaVuSans.ttf'
   }
}

var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);
var fs = require('fs');

// reading parameters from the command line
var params = process.argv;
var data = [];
data["invoicenumber"] = params[2];
data["buyeraddress"] = params[3]
data["item"] = params[4];
data["price"] = params[5];

// json with invoice layout 
var docDefinition = {
    content: [
        {
            fontSize: 11,
            table:{
                widths: ['50%','50%'],
                body:[
                    [{text:'Status: unpaid', border:[false,false,false,true],margin:[-5,0,0,10]},{text:'Invoice# '+data.invoicenumber, alignment:'right', border:[false,false,false,true],margin:[0,0,0,10]}]
                ]
            }
        },
        {
            layout: 'noBorders',
            fontSize: 11,
            table:{
                widths: ['50%','50%'],
                body:[
                    [{text:'Website.com',margin:[0,10,0,0]},{text:'Invoice date: '+data.invoicedata, alignment:'right',margin:[0,10,0,0]}],
                    ['...',''],
                    ['...',''],
                    ['...','']
                ]
            }
        },
        {
            fontSize: 11,
            table:{
                widths: ['50%','50%'],
                body:[
                    [{text:' ', border:[false,false,false,true],margin:[0,0,0,10]},{text:'Payment amount: $'+data.price, alignment:'right', border:[false,false,false,true],margin:[0,0,0,10]}]
                ]
            }
        },
        {
            layout: 'noBorders',
            fontSize: 11,
            table:{
                widths: ['100%'],
                body:[
                    [{text:'User account for payment:',margin:[0,10,0,0]}],
                    [data.buyerinfo],
                    [data.buyeraddress],
                    ['Payment link:'],
                    [{text:'https://website.com/shopcart/invoices_view.php?hash='+data.hash,margin:[0,0,0,10],fontSize:10}]
                ]
            }
        },
        {
            fontSize: 11,
            table:{
                widths: ['5%','56%','13%','13%','13%'],
                body:[
                    [{text:'Pos', border:[false,true,false,true]},{text:'Item', border:[false,true,false,true]},{text:'Price', border:[false,true,false,true]},{text:'Quantity',alignment:'center', border:[false,true,false,true]},{text:'Total', border:[false,true,false,true]}],
                    [{text:'1', border:[false,true,false,true]},{text:data.item, border:[false,true,false,true]},{text:"$"+data.price, border:[false,true,false,true]},{text:'1',alignment:'center', border:[false,true,false,true]},{text:"$"+data.price, border:[false,true,false,true]}]
                ]
            }
        },
        {
            layout: 'noBorders',
            fontSize: 11,
            margin:[0,0,5,0],
            table:{
                widths: ['88%','12%'],
                body:[
                    [{text:'Subtotal:', alignment:'right',margin:[0,5,0,0]},{text:'$'+data.price,margin:[0,5,0,0]}],
                    [{text:'Tax %:', alignment:'right'},'$0.00'],

                ]
            }
        },
        {
            fontSize: 11,
            table:{
                widths: ['88%','12%'],
                body:[
                    [{text:'Total:', alignment:'right', border:[false,false,false,true],margin:[0,0,0,10]},{text:'$'+data.price, border:[false,false,false,true],margin:[0,0,0,10]}]
                ]
            }
        },
        {
            layout: 'noBorders',
            fontSize: 11,
            alignment: 'center',
            table:{
                widths: ['100%'],
                body:[
                    [{text:'Wire transfer info:',margin:[0,10,0,0]}],
                    ['SWIFT: ...'],
                    ['Account number: ...'],
                    ['Company name: ...'],
                    [' '],
                    ['Company address:'],
                    ['...']
                ]
            }
        }
    ]

};
var options = {};

// create invoice and save it to invoices_pdf folder 
var pdfDoc = printer.createPdfKitDocument(docDefinition, options);
pdfDoc.pipe(fs.createWriteStream('invoices_pdf/'+data.invoicenumber+'.pdf'));
pdfDoc.end();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now we are ready to create PDF files. In our case we run a cron once a day that is a PHP file that creates and emails invoices. Here is a sample PHP code:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$param1 = "62384";
$param2 = "1600 Pennsylvania Avenue NW";
$param3 = "PHPRunner Enterprise Edition";
$param4 = "$999.00";
exec("\"c:/Program Files/nodejs/node.exe\" pdfmake/index.js ".$param1." ".$param2. " ". $param3." ".$param4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>node</category>
      <category>pdf</category>
    </item>
  </channel>
</rss>
