首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将新创建的PDF发送到浏览器?

如何将新创建的PDF发送到浏览器?
EN

Stack Overflow用户
提问于 2018-08-31 07:48:34
回答 1查看 33关注 0票数 1

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

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

我想我必须走一条api路线,但我不确定。有什么建议吗?

代码语言:javascript
复制
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 
EN

回答 1

Stack Overflow用户

发布于 2018-08-31 08:35:50

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52106766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档