Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
node.js html-pdf dont load external files from absolute path

In my app external files provided by absolute path are not loaded properly, but in my template its all good. html-pdf v.2.2.0 https://www.npmjs.com/package/html-pdf/v/2.2.0

I provide a href to my styles template, but they are not loading during converting my HTML template into PDF file. It appears to have raw styles. Same thing goes with images, there are just question marks in the places of images.

<link rel="stylesheet" type="text/css" href="https://api.greenleafpolska.pl/public/sources/index.css">

In my app I'm serving static files through these line:

app.use('/public', express.static(path.join(__dirname, 'public')))

code generating the PDF file:

app.get("/", (req,res)=>{
    fs.readFile('./template/index_min.html', 'utf8', function(error, data) {
        if (!fs.existsSync(`./tst`)){
            fs.mkdirSync(`./tst`);
        }
        fs.writeFile('./tst/out_min.html', data,
                         function (error){
                if (error) throw error;
            });
        pdf.create(data, { format: 'Letter'})
        .toFile('./tst/' + 1 + '_min.pdf', function(err, result) {
            
            var file = fs.createReadStream('./tst/' + 1 + '_min.pdf');
            var stat = fs.statSync('./tst/' + 1 + '_min.pdf');
            res.setHeader('Content-Length', stat.size);
            res.setHeader('Content-Type', 'application/pdf');
            res.setHeader('Content-Disposition', 'attachment; filename=' + 1 + '_min.pdf');
            
            file.pipe(res);
            

        });
    });
})

Is there an other way to provide external files in template ? I have tried "base" html tag, but results are the same. Is this the library issue ?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda