I'm quite new to Javascript.
I want to make a very simple native PDF viewer using nw.js and pdf.js. It reads a package file and extract a buffer that contains a valid PDF file. Then I convert the buffer into a stream like this:
var stream = require('stream');
var bufferStream = new stream.PassThrough();
bufferStream.end(pdfbuffer); // pdfbuffer is the PDF file's buffer
But I don't know how to make a valid URL for this PDF file so that I can fill it in pdf.js' default viewer code here:
// viewer.js from PDF.JS
var DEFAULT_URL = 'compressed.tracemonkey-pldi-09.pdf';
Or am I thinking of the wrong direction?
I think you should create a Writable stream first. Then write that stream to a PDF file which you will create too. I'm not sure if you can use a PDF file as a writable stream directly. Sorry if this doesn't really answer the question but take this as a nudge in the right direction.
Related
I am working with Blazor Server in a .NET6 application, which obviously is not super JavaScript friendly. However, it appears to me that the easiest way to download a PDF is through JavaScript, so I'm using an interop to make it happen.
My PDF files are stored in a FileTable on SQL. I can pull the data quite easily into my C# code. I am also storing JPG, PNG, TXT files, etc, on the file table and these are all downloadable using the following code in my interop.
async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer], {type: 'application/pdf'});
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
};
Passed into my interop: The string value of the name of my PDF file, and a MemoryStream of the byte array stored in the SQL FileTable.
All file downloading works fine, regardless of the type that I set on my Blob. Downloading of the PDF seems like it works from the outside, since the download occurs without incident and the file size is identical, but opening the file results in an error. I suspect that it has something to do with header information, but I'm not entirely sure and nothing I'm finding is helping here.
The above code was found in other StackOverflow answers, and like I said, works quite nicely for everything that is not a PDF. Is there an easy solution here, or do I need to look elsewhere?
I have tried tweaking the code to do a few different things based on different answers I find, and nothing has helped make the PDF actually work, although the downloading of other files is surprisingly resilient to change.
i am using electron and node js and trying to decrypt an encrypted file using crypto js, but i don't want to save it on user's local hard drive, i want to display it on video tag as i am reading it, meaning i don't want to use fs.createWriteStream after decryption, here is what i tried:
const decipher = crypto.createDecipher('des-ecb', 'a password');
const decInput = fs.createReadStream("encrypted video");
var file = decInput.pipe(decipher);
var file2 = fs.createReadStream(file);
is such thing is even possible? if i want to display the video i must have its url for source of video tag, but i can't make an URL for that file, and i cant even read it because it says object is passed to the fs.createReadStream and it says param must be string(path) or buffer or etc...
NOTE: the encryption and decryption works just fine so i ignored the rest of the code...
Similar question might already suit your needs. You already have the stream ready, let frontend reads it so that video is stored in the browser.
Base idea: I am creating an app. In which user chooses local file(mp4) in input field(type="file") and then streams the video to other user.
I am thinking to manipulate the file in javascript. And send it chunk by chunk to another user via(datachannels webRTC) then just play it on the other side chunk by chunk.
I understand that i can "assemble" the chunks using - MediaSource API
Questions: How can i split the video in chunks using javascript? I have been googling for a while i can't seem to find a library( maybe i am googling wrong keywords? ).
Thanks!
Use blob#slice to split the video
// simulate a file
blob = new Blob(['ab'])
chunk1 = blob.slice(0, 1)
chunk2 = blob.slice(1, 2)
console.log(blob.size)
console.log(chunk1.size)
console.log(chunk2.size)
Another thing i might think you are interested in is File streaming...
To get a ReadableStream from a blob you could use the hackish why of doing
stream = new Response(blob).body
reader = stream.getReader()
reader.read().then(chunk => spread(chunk))
Another cool library otherwise you can use to stream the blob Is by using Screw-FileReader
What wouldn't be more awesome then using WebTorrent to share the video got everything you need... uses WebRTC...
I'm using mPDF server side to create a pdf file. It works okay if I output the file to the server, however, I would like to return a string back to the client and build a pdf file from it which I can then use like any normal file from a file input.
server side, the (simplified) code is
$output_dest = 'S';
$content = $mpdf->Output($post_data->fileName, $output_dest);
//$mpdf->Output($post_data->fileName, 'F'); //just to check that the output should be correct
$response->setContent($content);
and client side i've tried using Blobs to create a file
var fileObj = new Blob([offerString], {type : 'application/pdf'});
but there are 2 problems. First, the blob, when sent to the server, doesn't have the required name. Secondly, the pdf file created (using window.saveAs to save the blob) is blank. It has the correct number of pages and author information, but it's completely blank.
If I use mPDF's file output, the resulting file is correct, so the problem must lie somewhere within the string->blob process.
Edit: The solution is to create the Blob not straight from the string but from an arrayBuffer. I've created the arrayBuffer using the solution suggested in another answer here
I am using jszip to create a zip file using javascript.And I want to zip a folder. I am using the following sample code to create zip file
var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n");
var img = zip.folder("images");
img.file("smile.gif", imgData, {base64: true});
var content = zip.generate();
location.href="data:application/zip;base64,"+content;
Using the above code the download popup is coming. But I want to save that zip file in a particular location without browse. How I can implement that. Thanks in advance for answer
Just for the information, zip.generate() has been removed in jsZip 3.0
You can not save zip or any other file at particular position using javascript or other scripts without browser, it's security violation, so as per my knowledge you can not do that at list by javascript.