Write PDF files from Web-App to USB-Stick

1.9k Views Asked by At

I am concerned with the feasibility of this: On a pre-configured machine I will have a Web-Application pre-installed, next to an Apache-Suite. So client and server are the same!

In this Web-Application Users can drag and drop PDF-Files to an USB-Icon.
Then the Web-App should write the dropped PDF to an attached USB-Stick.

I have never done something like this (writing to USB), so I am fairly insecure. And I am well aware of the browser-restrictions concerning JavaScript and Filesystem-Access, but...

after researching a bit I found out, that there might be some possible and
relevant (I'm a Web-Platform-Guy) solutions to this:

  • Make a "Chrome App" with USB-Permission (does this really work?)
  • Use PHP to find the USB and then write to it (how would that work under Windows?)
  • Use some Flash as middle man (not preferred)

Now I'd like to know:

  1. Has anyone some good experience with before mentioned possibilities?
  2. Has anybody ever done something similar? Did it work? Which path did you choose?
  3. How would I know which drive the USB is mounted, and how would I get sure?
  4. What other possible solutions to this problem are there?
3

There are 3 best solutions below

1
Jared Dykstra On

You have a website ('client-side' user interface) and a back-end server ('server-side') running on the same machine. This gives you 2 options:

  1. Client-side: Download a file through the browser via HTTP GET and let the user choose where they save it.
  2. Server-side: Build your USB interactions into the back-end (Node.js) code, as @mcgraphix suggests.

Interacting with the USB on the server-side provides the most flexibility. Furthermore, there are a number of libraries that you can leverage. Head to npmjs.org and consider, among others, the following Node.js server-side packages:

With the server-side approach, initiate a Webservice request when the user completes the drag & drop action on the client, and implement the USB interaction within the server (Express.js or similar) method which services the request.

1
Pawel On

If the letter of the stick is known then writing a file from PHP will be simple

file_put_contents( 'E:\\folder\\file.pdf', $data );

Update

You can read a list of drives into a dropdown and allow a user to select a default drive to write to

https://stackoverflow.com/a/8210132/696535

2
Daniel Lane On

Your question is more an architecture question than a code specific question.

Your web app (if you insist on a web app) should have two major components, a server side component that can be given arbitrary commands, and a client side component (javascript using XMLHttpRequest) that can make requests to the server side component to execute said arbitrary commands.

So your server side component, the component that serves your web page should have some server side code that can write your pdf to the file system, it should probably generate the pdf file as well rather than doing that on the web browser.

Which technology you use is up to you, whether that's PHP, .Net, Node.js etc...

The general gist is you want a server side framework that deals with HTTP requests, in your case probably a post request from the client side containing the encoded pdf, and responds accordingly. Bind a particular http route to trigger your save logic.

Your http post request to the server will contain your payload which is the pdf file to a particular path, e.g. http://localhost/savepdf that whichever technology stack http listens to (you'll need to configure that)

Your server side component should read the incoming data, decode it as appropriate then make a file system request to write the received payload to disk.