我想用热敏打印机在节点快递中创建帐单。
我尝试使用此工作代码,我的朋友说,使用转义字符时,对齐位置不正确。因此,如何将其创建为餐厅发票或账单?
我尝试过的事情
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."
},
}
答案 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}`))