带有json和jsPDF的发票PDF或账单PDF

时间:2018-11-13 09:29:58

标签: javascript node.js

我想用热敏打印机在节点快递中创建帐单。

我尝试使用此工作代码,我的朋友说,使用转义字符时,对齐位置不正确。因此,如何将其创建为餐厅发票或账单?

我尝试过的事情

var express = require('express');
var app = express();

app.get('/', function(req, res)
{
    global.window = {document: {createElementNS: () => {return {}} }};
    global.navigator = {};
    global.btoa = () => {};

    var fs = require('fs');
    var jsPDF = require('jspdf');

    var doc = new jsPDF();

    var sampleText = 'NO \t ITEM \t QTY \t PRICE \t AMOUNT \n \n 1 \t Sugar \t 90.00 \t 2.00 \t 180.00 \n \n 2 \t Rice  \t 80.00 \t 5.50 \t 440.00 \n \n 3 \t Biscuit \t 50.75 \t 7.00 \t 355.25 \n \n

    var data = doc.output(sampleText);

    fs.writeFileSync('./document.pdf', data);

    doc.text("Hello World", 10, 10);
    var data = doc.output();

    fs.writeFileSync('./invoice.pdf', data);

    delete global.window;
    delete global.navigator;
    delete global.btoa;
});

var port = process.env.PORT || 8080;
app.listen(port);
console.log('Server started');

module.exports = app;

账单样式

  • 应该采用热敏打印机的纸张尺寸(即:80毫米宽)。
  • 标题部分应居中(商店名称,地址,电话和日期/时间,柜台,账单编号)。
  • 带有虚线的水平分隔符。
  • 购买物品应循环到总购买物品中。
  • PRICE和AMOUNT应该正确对齐(所有货币值)。
  • 净总计,现金和余额一词应左对齐,而这些值应右对齐
  • 最后,页脚应居中。

结算格式

|               Shop Name               |
|                Address                |
|               Telephone               |
|                                       |
| 13/11/2018 14:18:49  IamCoder  No: 99 |
|---------------------------------------|
| NO |   ITEM  |  PRICE | QTY |  AMOUNT |
|:--:|:-------:|:------:|:---:|--------:|
| 1  | Sugar   |  90.00 | 2.00|  180.00 |
| 2  | Rice    |  80.00 | 5.50|  440.00 |
| 3  | Biscuit |  50.75 | 7.00|  355.25 |
|---------------------------------------|
| Net Total                      975.25 |
|                                       |
| CASH                          1000.00 |
| Balance                         24.75 |
|------------IMPORTANTNOTICE------------|
| In case of a price discrepancy return |
|   the bill and item within 2 day to   |
|         refund the difference         |

样本json

{
    "header": {
        "bill": "99",
        "shop": "Shop Name",
        "address": "Address",
        "telephone": "Telephone",
        "date": "13/11/2018 12:45:52",
        "counter": "IamCoder"
    },
    "items": [{
        "item": "Sugar",
        "price": "90.00",
        "qty":"2.00",
        "amount":"180.00"
    },
    {
        "item": "Rice",
        "price": "80.00",
        "qty":"5.50",
        "amount":"440.00"
    },
    {
        "item": "Biscuit",
        "price": "50.75",
        "qty":"7.00",
        "amount":"355.25"
    }],
    "footer": {
        "total":"975.25",
        "cash":"1000.00",
        "balance":"24.75",
        "notice": "In case of a price discrepancy, return the bill and item within 2 days to refund the difference."
    },
}

1 个答案:

答案 0 :(得分:0)

似乎jspdf在客户端生成pdf。我以前在服务器端使用过pdfkit并使用了express。

类似的东西:

const PDFDoc = require('pdfkit')
const express = require('express')
const app = express()
const fs = require('fs')

app.get('/', (req, res) => {
  const doc = new PDFDoc()
  doc.text('hello world')
  doc.pipe(fs.createWriteStream('out.pdf'))

  res.status(200).send('OK')
})

const PORT = process.env.PORT || 3001

app.listen(PORT, () => console.log(`app is running on port ${PORT}`))