OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Viewing a PDF file as a response looks weird in Postman

  • Thread starter Thread starter Dulan Jay
  • Start date Start date
D

Dulan Jay

Guest
I am sending a pdf file as a response to a POST request. But when I view the response in Postman, that looks weird as it's missing some spots. When I open that pdf in a browser it looks perfect.

Why is that? I don't think there is an error in code because when I open the pdf in browser, it is perfectly generated. Anyway I am attaching the code.

Code:
router.post('/getPdf', auth.authenticateToken, function (req, res) {
    const orderDetails = req.body;
    const pdfPath = './generated_pdf/' + orderDetails.uuid + '.pdf';
    if (fs.existsSync(pdfPath)) {
        res.contentType("application/pdf");
        fs.createReadStream(pdfPath).pipe(res);
    }
    else {
        var productDetailsReport = JSON.parse(orderDetails.productDetails);
        ejs.renderFile(path.join(__dirname, '', "report.ejs"), { productDetails: productDetailsReport, name: orderDetails.name, email: orderDetails.email, contactNumber: orderDetails.contactNumber, paymentMethod: orderDetails.paymentMethod, totalAmount: orderDetails.totalAmount }, (err, results) => {
            if (err) {
                console.log(err);
                return res.status(500).json(err);
            }
            else {
                pdf.create(results).toFile('./generated_pdf/' + orderDetails.uuid + ".pdf", function (err, data) {
                    if (err) {
                        console.log(err);
                        return res.status(500).json(err);
                    }
                    else {
                        res.contentType("application/pdf");
                        fs.createReadStream(pdfPath).pipe(res);
                    }
                })
            }
        })
    }

})

This is what I see

When I'm sending the request again and again, it's changes the blank spots only (position of spots changes).
<p>I am sending a pdf file as a response to a POST request. But when I view the response in Postman, that looks weird as it's missing some spots. When I open that pdf in a browser it looks perfect.</p>
<p>Why is that? I don't think there is an error in code because when I open the pdf in browser, it is perfectly generated. Anyway I am attaching the code.</p>
<pre><code>router.post('/getPdf', auth.authenticateToken, function (req, res) {
const orderDetails = req.body;
const pdfPath = './generated_pdf/' + orderDetails.uuid + '.pdf';
if (fs.existsSync(pdfPath)) {
res.contentType("application/pdf");
fs.createReadStream(pdfPath).pipe(res);
}
else {
var productDetailsReport = JSON.parse(orderDetails.productDetails);
ejs.renderFile(path.join(__dirname, '', "report.ejs"), { productDetails: productDetailsReport, name: orderDetails.name, email: orderDetails.email, contactNumber: orderDetails.contactNumber, paymentMethod: orderDetails.paymentMethod, totalAmount: orderDetails.totalAmount }, (err, results) => {
if (err) {
console.log(err);
return res.status(500).json(err);
}
else {
pdf.create(results).toFile('./generated_pdf/' + orderDetails.uuid + ".pdf", function (err, data) {
if (err) {
console.log(err);
return res.status(500).json(err);
}
else {
res.contentType("application/pdf");
fs.createReadStream(pdfPath).pipe(res);
}
})
}
})
}

})
</code></pre>
<p><a href="https://i.sstatic.net/znhjw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/znhjw.png" alt="This is what I see" /></a></p>
<p>When I'm sending the request again and again, it's changes the blank spots only (position of spots changes).</p>
Continue reading...
 

Latest posts

A
Replies
0
Views
1
Anthonin Chaboteaux
A
Top