1

I have the following function which is expected to run and generate PDF from the link :

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('https://www.google.com/', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function (err, res) {
    if (err) return console.log(err);
    console.log(res); // { filename: '/app/businesscard.pdf' }
});

However when I run it , I get the following error from my terminal :

internal/fs/utils.js:454
    throw err;
    ^

Error: ENOENT: no such file or directory, open 'https://www.google.com/'
    at Object.openSync (fs.js:436:3)
    at Object.readFileSync (fs.js:336:35)
    at Object.<anonymous> (C:\Users\hdindi\Documents\NetBeansProjects\NodeJsApplication\main.js:9:15)
    at Module._compile (internal/modules/cjs/loader.js:759:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:822:10)
    at internal/main/run_main_module.js:17:11

How can I resolve this ?

H Dindi
  • 1,484
  • 6
  • 39
  • 68
  • You need to send a http request to read the webpage, you can't use the `fs` module for that. – eol Jul 23 '19 at 19:39

1 Answers1

0

You need to send a http request to the webpage you want to export as pdf first, then pass the read html-content to your pdf-generation library. Something like this (using request-promise to fetch the scrape the html, note that there's no error handling yet):

const fs = require('fs');
const pdf = require('html-pdf');
const rq = require('request-promise');
const html = await rp('http://www.google.com');

const options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
});
eol
  • 23,236
  • 5
  • 46
  • 64