Hi everybody I am facing an issue where when I create and save pdf contents to a pdf file it always opens up blank here is my code snippet
i am using express.js
fs.writeFile(
"response.pdf",
pdfData.data,
{ encoding: "utf-8" },
function (err) {
if (err) throw err;
console.log("File is created successfully.");
}
); ```
here is an example of the pdf data I'm trying to add to the pdf
```%PDF-1.5
%����
3 0 obj
<</ColorSpace/DeviceRGB/Subtype/Image/Height 224/Filter/F...(more characters)````
please advice
Related
When I try to read a file that is stored in the database, node gives me an error saying:
"error": "ENOENT: no such file or directory, open 'http://localhost:8000/assets/uploadNk2i8BAE81Wp6pFI6TtF-.pdf'"
But, when I visit that link in the browser, I can see the pdf file. So, why is node failing to read the file?
This is my code:
fs.readFileSync(req.user.resume, 'utf-8', (err,data) => {
if(err) {
res.send(err);
}
else {
res.send(data);
}
})
I have currently a problem to fetch my pdf from the backend to the front-end.
I'm using NodeJS.
I have all my pdf made with Puppeteer and saved on a PDF directory.
I'm trying to fetch these PDF for the front-end. (for download)
I'have tried with res.sendFile and res.download (res.sendFile(path.join(__dirname,"/path"), res.download(path.join(__dirname,"/path))
This is the response from the front-end:
{data: "%PDF-1.4↵%����↵1 0 obj↵<</Creator (Chromium)↵/Prod…e 7↵/Root 6 0 R↵/Info 1 0 R>>↵startxref↵675↵%%EOF", status: 200,...}
I can't download this files.
Image
Code from back-end :
try {
res.download( path.join(__dirname, '/PDF/sofianddes.pdf'), (err) => {
if (err) {
console.log(err);
}
});
} catch (err) {}
Someone could help me ?
Thanks
I've been stuck on this problem for awhile now and can't seem to figure out why this file isn't being uploaded to firebase correctly. I'm using the code below in firebase functions to generate a document, then I convert that document to a stream, finally I create a write stream declaring the path that I want the file to be written to and pipe my document stream to the firebase storage WriteStream.
Example 1: PDF uploaded from file system through firebase console. (Link works and displays pdf)
Example 2: PDF generated in firebase functions and written to storage using code below
Considerations:
I know the PDF is valid because I can return it from the function and see it in my web browser and everything is as I would expect.
When trying to open the bad file it doesn't display anything and redirects me back to the overview screen.
const pdfGen = require("html-pdf");
pdfGen.create(html, { format: "Letter" }).toStream(function (err, stream) {
if (err) return res.status(500).send(err);
var file = storage.bucket()
.file(job.jobNum + "/quote-doc.pdf")
.createWriteStream({
resumable : false,
validation : false,
contentType: "auto",
metadata : {
'Cache-Control': 'public, max-age=31536000'}
});
stream.pipe(file)
.on('error', function(err) {
return res.status(500).send(err);
})
.on('finish', function(data) {
stream.close();
file.end();
res.end();
console.log('finished upload');
});
});
Hi i'm trying to download a pdf with nodejs (UNIREST) but i'm a bit stocked.
var url_pdf = "http://myurl.com/ex.pdf"
unirest.get(url_pdf)
.as.binary(function (response) {
fs.writeFile("./pdf/test.pdf", response.body, 'binary', function (err){
if (err)
console.log(err);
});
})
When wrting the file, its miss formatted (just a blank pdf file) size is ok.
In the current project I am working with sails js as back end and angular js as front end. I have implemented a cropping module. By current problem is that, the output of the cropping module is base64 data. For doing some manipulations I need to convert this base 64 data to an image file. I received the base64 data at server side. Now need to convert this data to a file in sails js server side. I used a code for that, but not creating the image file.
My sample base 64 data is below
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4Xuy9B6wkeX4e9lVVd1Xn3P1yDpNz2J3N+e72juFODHfH......................."
my code for converting this base64 to image file is
var image = req.body.image.replace(/^data:image\/jpeg;base64,/, "");
var filePath = 'imagecontents/';
fileName = filePath +'abc.jpg';
mkdirp(path.join(__dirname, '../../..' + filePath), function (err) {
if (err) {
logger.log('error', err);
res.sendStatus(500);
throw err;
}
require("fs").writeFile(path.join(__dirname, '../../..' + fileName), image, 'base64', function (err) {
if (err) {
logger.log('error', err);
res.sendStatus(500);
throw err;
}
});
Plz help to rectify is there is any error in in this code
You can write the file using the below code:
var base64Data = data.replace(/^data:image\/png;base64,/, "");
require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
console.log(err);
});