Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有json和jsPDF的发票PDF或账单PDF_Javascript_Node.js - Fatal编程技术网

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

Javascript 带有json和jsPDF的发票PDF或账单PDF,javascript,node.js,Javascript,Node.js,我想用热敏打印机在node express中创建账单 我尝试了这个工作代码,我的朋友说通过使用转义字符,对齐位置不正确。那么,我怎样才能像餐馆发票或账单一样创建它呢 我尝试的 var express = require('express'); var app = express(); app.get('/', function(req, res) { global.window = {document: {createElementNS: () => {return {}} }}

我想用热敏打印机在node express中创建账单

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

我尝试的

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

|               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。我以前在服务器端使用过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}`))

如果你能以某种方式将json转换为html,那么请转到此链接[但我如何循环项目?在客户端或服务器端创建pdf是可以的。但我如何才能像账单一样获得它。使用for text和drawing tables与for table的组合?它是否负责热敏打印机的宽度(75 mm)嗯,你可以用定义pdf页面宽度。我感兴趣的是,在你创建pdf之后,你真的可以打开它吗?不!我也等着告诉你,但是忘记了。pdf已创建但未打开(崩溃)。