gpt4 book ai didi

javascript - 如何将新建的PDF发送到浏览器?

转载 作者:行者123 更新时间:2023-11-30 20:13:59 25 4
gpt4 key购买 nike

我正在使用 pdf 包创建交易收据的 pdf。

我可以使用终端在我的文件夹中生成 pdf,但我试图通过单击 html 在浏览器中生成 pdf。

我想我必须做一个 api 路由,但我不确定。有什么建议吗?

var sys = require('sys');
var fs = require('fs');
var pdf = require('pdf').pdf;
var moment = require('moment');
var Decimal = require('decimal');

/* create the PDF document */
var item = "Vinyl";
var numItem = 3
var price = Decimal(30.00);
var subTotal = Decimal(numItem).mul(price).toNumber();
var tax = Decimal(.07).mul(subTotal).toNumber();
var total = tax + subTotal;

var doc = new pdf();
doc.text(80, 20, 'Transaction Reciept' );
doc.text(80, 30, 'Hot Drop Vinyl Shop');
doc.text(70, 40, moment().format('MMMM Do YYYY, h:mm:ss a'));
doc.text(50, 60, 'Item(s): ' + item);
doc.text(50, 70, 'Quantity: ' + numItem);
doc.text(50, 80, 'Price: $' + price);
doc.text(50, 90, 'Subtotal: $' + subTotal);
doc.text(50, 100, 'Tax: $' + tax);
doc.text(50, 110, 'Total: $' + total);


/* optional - set properties on the document */
doc.setProperties({
title: 'Transaction Reciept',
subject: 'Reciept for Transactions at Vinyl Shop',
author: 'Group 6',
keywords: 'Vinyl Shop, Coding Bootcamp',
creator: 'pdf.js'
});

var fileName = "testFile"+new Date().getSeconds()+".pdf";

fs.writeFile(fileName, doc.output(), function(err, data){
sys.puts(fileName +' was created! great success!');
});

//send file to browser

最佳答案

这取决于您要实现的目标。你可以查看pdfmake ( https://www.npmjs.com/package/pdfmake ) 或 jsPDF ,这些是直接在浏览器中生成 pdf 的好库。

但是,如果这不能满足您的需求,那么是的,您将不得不使用像 express 这样的节点网络框架。创建一个生成 pdf 并将其发送到浏览器的 api 路由。

关于javascript - 如何将新建的PDF发送到浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52106766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com