javascript - 带有 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;
比尔风格
- 应采用热敏打印机的纸张尺寸(即:80 毫米宽)。
- 标题部分应居中(商店名称、地址、电话和日期/时间、计数器、帐单编号)。
- 带有虚线的水平分隔符。
- 购买项目应该循环到总购买项目。
- PRICE 和 AMOUNT 应右对齐(所有货币值)。
- Net Total、CASH 和 Balance 这个词应该左对齐,而这些值应该右对齐
- 最后,页脚应该居中。
账单格式
| 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}`))
推荐阅读
- database - 降级后 MongoDB 未启动
- excel - VBA 脚本:代码在调试模式下运行良好,但不是在完全运行模式下
- python - 如何搜索数据框并相应地对每个数据框进行排序?
- azure - 自动将更改从一个 Azure 数据库应用到另一个
- php - 如何合并这个 sql server 查询?
- c# - 简单的查找素数实现
- angular - 如何以角度显示工具提示
- go - 是否可以在没有内置库的情况下编译可以在没有操作系统的机器上运行的 go 代码
- mysql - MYSQL UNIX_TIMESTAMP('02/05/2019') 是不确定的吗?
- c# - Blazor 的“入门”问题