Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: fs.createWriteStream is not a function

I want to make a pdf file on client-side and afterwards it has to be send server-side through a REST call.

With the generation of the file, I have no problem. I use pdfMake to generate the file and that works fine.

Because I want to send the file to my backend, I have to store the file on the filesystem. To achieve this, I use node.js. The code below works fine. Only when I want to use a function of the object fs, I get the error 'Uncaught TypeError: fs.createWriteStream is not a function'. What am I doing wrong?

var docDefinition = localStorage.getItem('docDefinition');  
var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter();

var pdfDoc = printer.createPdfKitDocument(JSON.parse(docDefinition));

var fs = require('fs');
var stream = fs.createWriteStream('basics.pdf');
pdfDoc.pipe(stream);
pdfDoc.end();
like image 702
user3193324 Avatar asked Jun 01 '16 13:06

user3193324


1 Answers

You are confusing the server and the client. The browser has no programmatic access to the filesystem. Period. The filesystem api is triggered only via user interaction (like dragging and dropping a file on the webpage). You cannot write a file locally on the client.

like image 67
Jared Smith Avatar answered Nov 16 '22 00:11

Jared Smith