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.
Related
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
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');
});
});
I want get content from my uploaded file. Uploaded file is just text file and i want read this file line by line
req.file("model").upload(function (err, uploadedFiles){
if (err) return res.serverError(err);
return res.json({
message: uploadedFiles.length + ' file(s) uploaded successfully!',
files: uploadedFiles,
content: uploadedFiles[0] // I want to get my uploaded file content
// Stream or buffer
});
});
You can get the file descriptor from uploadedFiles[0].fd
Use it to read / stream the file.
fs.readFile(uploadedFiles[0].fd, 'utf8', function (err,data) {
return res.json(200, {message: 'Ok', data: data});
})
First install Local filesystem streaming binary adapter for Sails.js / Waterline
npm install sails-local-fs
in your code create fs instance
var fs = require('fs');
And now use readFile() method to get content of your file
fs.readFile(files[0].fd, 'utf8', function (err,data) {
return res.json(200, {message: 'Ok', data: data});
})
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);
});
I'm trying to insert a binary attachment to CouchDB with nano. I have a JPG in data returned by http.request.
I save it with nano as follows
db.attachment.insert( id, 'content', self._data, contentType, {rev: rev}, function(err, body) {
callback();
});
but when I try view it though a web browser the image is broken.
The file is full of UTF-8 escape characters which is visible when I pull it with CURL:
$ curl http://127.0.0.1:5984/web-crawler/doc-test.jpg/content
"ÿØÿà\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000H\u0000H\u0000\u0000ÿâ\fXICC_PROFILE\u0000\u0001\u0001\u0000\u0000\fHLino\u0002\u0010\u0000\u0000mntrRGB XYZ \u0007Î\u0000\u0002\u0000\t\u0000\u0006\u00001\u0000\u0000acspMSFT\u0000\u0000\u0000\u0000IEC sRGB\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000öÖ\u0000\u0001\u0000\u0000\u0000\u0000Ó-HP \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\
Content is not corrupted because if I save it to a file I can see the image.
var fs = require('fs');
fs.writeFile('logo.jpg', data, 'binary', function(err){
if (err) throw err
console.log('File saved.')
});
What is the right way to do it?
Ok, this question can be close. The answer to my problem is Buffer:
db.attachment.insert( id, 'content', new Buffer(self._data, "binary"), contentType, {rev: rev}, function(err, body) {
callback();
});