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

Javascript 如何将新创建的PDF发送到浏览器?,javascript,pdf,Javascript,Pdf,我正在使用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 t

我正在使用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 

这取决于你想要达到的目标。您可以签出()或者,这些是直接在浏览器中生成pdf的好库

但是,如果这不符合您的需要,那么您将不得不使用节点web框架来创建api路由,生成pdf并将其发送到浏览器