Skip to content

Sending the pdf file without saving #1414

@suri1996

Description

@suri1996

Thank you for submitting an issue to jsPDF. Please read carefully.

Are you using the latest version of jsPDF?

Have you tried using jspdf.debug.js?

Steps to reproduce
I am using this library for generating a PDF receipt on the server side.

Here is my code:

const getReceipt = (req, res) => {
logger.info("getReceipt", {query: req.query});
if (!req.query.receipt) { res.status(400).end(); return; }
return db.get(req.query.receipt, (err, value) => {
if (err) { res.status(404).end(); return; }
// res.json(value);
var doc = new jsPDF();
res.set("Content-Type", "application/pdf");
//res.json({tx: JSON.parse(value)});
var dox = JSON.parse(value);
doc.setFontSize(30);
doc.text(90, 20, "Receipt");
doc.setFontSize(20);
doc.text(20, 40, "Order");
doc.setFontSize(15);
var len = dox['items'].length;
var y = 60;
var total = 0;
for(var j=0; j<len;++j) {
var price;
var quantity;
for(var key in dox['items'][j]) {
doc.text(20, y, key.replace("", " "));
doc.text(170, y, dox['items'][j][key].toString());
if(key=="unit_price") { price = dox['items'][j][key]; }
if(key=="quantity") { quantity = dox['items'][j][key]; }
y=y+10;
}
doc.text(110, y, "Subtotal");
doc.text(170, y, (pricequantity).toString());
total += price
quantity;
y=y+20;
}
doc.text(110, y, "Total");
doc.text(170, y, total.toString());
y = y+20;
doc.setFontSize(20);
doc.text(20, y, "Payment");
y = y+20;
doc.setFontSize(10);
if (!("details" in dox))
dox["details"] = {};
else if (typeof(dox["details"])==="string")
dox["details"] = JSON.parse(dox["details"]);
if (!("payment" in dox["details"]))
dox["details"]["payment"] = {};
if (!("messages" in dox["details"]["payment"]["messages"]))
dox["details"]["payment"]["messages"] = {}
for(var key in dox['details']['payment']['messages']) {
if(key[0]!=='
') {
doc.text(20, y, key.replace("_", " "));
doc.text(130, y, dox['details']['payment']['messages'][key].toString());
}
y+=10;
}
res.send(doc);
return;
});
};
When I try to save the pdf it works perfectly, however, when I try to return it, it gives this error: Converting circular structure to JSON, and it gives this error at the line: 'res.send(doc)'. I know this is not an issue, I wasn't sure if this library is capable of doing this in the first place, but some help would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions