I'm using PDFKit (A PDF generation library for Node.js, pdfkit.org) and I want to send a PDF as a response to a client.
# Write the PDF file to disk
doc.write('output.pdf');
The code above writes the PDF file to the disk, but I want it to send as the response. How can I do that?
Assuming res is your server response object, just do this:
doc.output(function(string) {
res.end(string);
});
This will send a string representation of the PDF rather than writing it to file. The code above is the compiled CoffeeScript that was in the documentation for PDFKit.
doc.output (string) ->
console.log string
Related
I upload a file using the file from the browser using the file type input. I have a system, where I receive that file on the client side read the binary of it and then convert it to base64 (using btoa), do some more stuff as part of my system and then the system uploads it to the remote Apache server as a Multipart request.
When I download the file, I do the reverse, parse the response, get the raw string, convert it to base64, send that string to client side and convert that base64 string into binary again (using atob) and save the file using octet-stream type using Blob constructor and the createObjectURL method. The file is saved on the disk, so no problem here. When I open the file it's content is unreadable.
I did some research and compared my base64 during upload with the base64 during the download part and I see that the base64 string gets corrupted in the download process.
So, for example, I am just making up the strings, but the intent is to show what is happening
Upload base64:
UIABvcfce+tre2df
Download corrupted base64:
UIABvcfDuuuvvv55rrre2df
You would see as shown above ce+t is changed to Duuuvvv55rr.
And therefore, my question - How to remove the mutation (or corruption) from a base64 string?
I tried a lot of search on stackoverflow and other online platform with direct or indirect references but somewhere it is not working.
I created a PDF of a website using puppeteer from a link provided by the client, and now I need to send back as response the stream code/ octet stream / binary data of this pdf, I am unsure how to do that can someone help me
I have the PDF stored locally in the same folder as my js file
Just use
response.body = fs.readFileSync("path/to/file.pdf");
in KOA.
This will automatically send the pdf file as an application/octet-stream.
I'm currently working on a Ionic project. We want to transfer Files using authentication, so I do a request to the server with my session token in the header and then the server returns a piece of JSON code. This JSON contains the file type (.png, .docx, etc) and a base64 string of the file.
When I write this base64 string to a file I get the exact same string in the file. However, if I use the atob() function to decode it, I get a corrupted file. When I open this file in notepad, I can see that more than 99% of the file is the same as the original. Only a few characters are different. Does somebody know a solution to this?
The File is uploaded via a web portal to the PHP framework. When we use the PHP code to get the file and decode the base64 string to a file, it works just fine. (PHP framework returns in UTF-8).
$http({method: "GET", url: "PATH TO SERVER" + students[y].id + ".json"}).then (function(result) {
$cordovaFile.writeFile(cordova.file.externalCacheDirectory, fileName + result.data.data.data(filetype), result.data.data.base64, true).then(function(res) {
console.log(res);
});
})
I saving data in database(sql server) using Latin1. My html application uses utf-8 encoding.
Server side is wrote in c# using WebApi.
When I try to download file from db which contains word ' 123' in txt file:
$http.get<string>(url).success(data=> console.log(data);
I get this: �123
After use this
unescape(encodeURIComponent('�123'));
I get "�123"
How can to convert returned string from server to proper one ?
I'm trying to save the result of a XMLHTTPRequest to a PDF file.
I am communicating with a server I am calling to get a chunk of data formatted as PDF data.
I'm using XMLHTTPRequets to log into the server, then make a search request which in return creates a PDF which is streamed back to me though the XMLHTTPRequest.
I need to save that result as a PDF so I can later open it in Acrobat.
When I save the response text to a file the result is not a valid PDF. The request is doing something to the stream which makes it invalid as a PDF.
I have no control over that server so I cant make it send back a link to a temporary valid PDF file.
Is there a way around that ?
Is there a way to encode that stream into a valid PDF file ?
I am using Javascript for that application.
Thanks
Erez
what you could do is return from your http server an xml file like the following:
<resp><![CDATA[YOURPDFSTREAM]]></resp>
and them retrieve the RESP node to build your pdf file.
Don't forget to encode your stream in the desired encoding format as well.