Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda