Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

826
Views
How to add image with pdfmake

I need to add an image in the header of the report generated by pdfmake. But when following the documentation I can't make this insertion, not even when converting the image to base64. Please suggest another solution

What was done:

import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';

function epiPDF(epis){
    pdfMake.vfs = pdfFonts.pdfMake.vfs;

    const header = [
        {
            text: 'Description equipament',

            bold: true,
            fontFamily: 'Roboto',
            decoration: 'underline',

            margin: [0, 20, 0, 0],
            alignment: 'center',

            image: 'data:image/jpeg;base64,/9j/END_DATAURL_BASE64'

        }
    ];
    function Rodape(currentPage, pageCounf){
        return [
            {
                text: currentPage + ' / ' + pageCounf,
                alignment: 'right',
                fontSize: 9,
                margin: [0, 10, 20, 0]
            }
        ]
    }

    const docDefinitios = {
        pageSize: 'A4',
        pageMargins: [10, 50, 10, 40],

        header: [header],
        content: [infor, rec, details],
        footer: [Rodape]
    }
    pdfMake.createPdf(docDefinitios).download();
}

export default epiPDF;

when testing base64 code is correct

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Could you please indicate what the browser errors are ?

Also I suggest you try again with the base64 encoded image specified in the pdfmake's playground

about 4 years ago · Juan Pablo Isaza Report

0

  1. Your docDef's header and footer should be functions instead of Arrays.

  2. Pdfmake elems have a specific structure, for example a text element cannot have an image (it must be 2 separated elems).

Your code should look something like this :

 const getHeader = (currPage, totalPage) => [
    {
        text: 'Description equipament',

        bold: true,
        fontFamily: 'Roboto',
        decoration: 'underline',

        margin: [0, 20, 0, 0],
        alignment: 'center',

        image: 'data:image/jpeg;base64,/9j/END_DATAURL_BASE64'

    },
    {
     image: 'data:image...YOUR IMAGE',
     margin: [0, 20, 0, 0],
     alignment: 'center',
    }
  ]

 const docDefinitios = {
    pageSize: 'A4',
    pageMargins: [10, 50, 10, 40],

    header: getHeader,
    content: [infor, rec, details],
    footer: Rodape
}

Cheers

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!