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.
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 trying to use the JSSoundRecorder(https://github.com/daaain/JSSoundRecorder) for recording a file on my website. I would like to send the resulting wav along with a form where I add more info. Is there a way to save the Blob in my form? So far I can only find a way to directly send the blob to the server using a Ajax request. Is there a way to put the result of the recorder in a hidden file or another smart solution?
You won't be able to upload it as a file [1] (via <input type="file">) with a form, but you could try to base64 encode it and submit it via <input type="text">. You'd then have to decode it on the server before writing it to a file. This JSPerf has a few different methods of converting a Blob to base64: http://jsperf.com/blob-base64-conversion
[1] How to set a value to a file input in HTML?
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
I'm using FileReader.readAsBinaryString to upload a file using a multipart/form-data POST request to a server.
The file gets sent and the server receives and saves the file.
When I try to open the file on the server I get messages saying that it is corrupted (png images) or I see a blank document (in the case of a pdf). Obviously something is going wrong.
Is there some other encoding that needs to be applied to the data returned in event.target.result in the FileReader.onload handler? Am I missing something else?
Thanks
Try using FormData instead of reading the file as a binary string and constructing the multipart/form-data request manually. See my response here:
HTML5 File API readAsBinaryString reads files as much larger, different than files on disk