首页 > 解决方案 > 带有 json 和 jsPDF 的发票 PDF 或账单 PDF

问题描述

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

我尝试使用此工作代码,我的朋友说通过使用转义字符,对齐位置不正确。那么,我怎样才能像餐厅发票或账单一样创建它呢?

我试过的

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;

比尔风格

账单格式

|               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."
    },
}

标签: javascriptnode.js

解决方案


似乎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}`))

推荐阅读