javascript - 使用 json 和 jsPDF 生成发票 PDF 或帐单 PDF

标签 javascript node.js

我想使用热敏打印机在 Node Express 中创建帐单。

我尝试使用这个工作代码,我的 friend 说通过使用转义字符,对齐位置不正确。那么,我该如何创建它,就像餐厅发票或帐单一样?

我尝试过的

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 毫米宽度)。
  • 标题部分应居中(商店名称、地址、电话和日期/时间、柜台、账单编号)。
  • 带有虚线的水平分隔符。
  • 购买商品应循环至购买商品总数。
  • 价格和金额应右对齐(所有货币值)。
  • “净总额”、“现金”和“余额”一词应左对齐,而这些值应右对齐
  • 最后,页脚应居中。

账单格式

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

最佳答案

看来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}`))

关于javascript - 使用 json 和 jsPDF 生成发票 PDF 或帐单 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53277786/

相关文章:

javascript - 将 CSS3 滤镜应用于 Leaflet 上的图像 block

javascript - nuxt.config.js 未加载 .css 文件

android - Phonegap 安装/构建错误

javascript - 类型错误 : Cannot set property 'auto' of null

javascript - 如何将现有回调 API 转换为 Promise?

linux - 对象 <#BeagleBone> 没有方法 'i2cConfig' 尝试使用 Beaglebone-IO 运行 MPU6050

javascript - 动态添加复选框时,用于从组中选择一个复选框的 JQuery 脚本不起作用

javascript - 文本中断javascript中的单引号

javascript - 必须返回有效组件错误

node.js - 在 Express 的自定义错误处理程序中获取堆栈跟踪?