Pdfmake: How to give background color to pdf file

Created on 10 Feb 2017  路  6Comments  路  Source: bpampuch/pdfmake

I want to give navy light blue color through out pdf backgound color

feature request page

Most helpful comment

Workaround:

var dd = {
    pageSize: {
      width: 595.28,
      height: 841.89  
    },
    background: function () {
        return {
            canvas: [
                {
                    type: 'rect',
                    x: 0, y: 0, w: 595.28, h: 841.89,
                    color: '#00BFFF'
                }
            ]
        };
    },
    content: [
        {text: 'Simple text 1', pageBreak: 'after'},
        {text: 'Simple text 2', pageBreak: 'after'},
        {text: 'Simple text 3', pageBreak: 'after'}
    ]
};

All 6 comments

+1

Workaround:

var dd = {
    pageSize: {
      width: 595.28,
      height: 841.89  
    },
    background: function () {
        return {
            canvas: [
                {
                    type: 'rect',
                    x: 0, y: 0, w: 595.28, h: 841.89,
                    color: '#00BFFF'
                }
            ]
        };
    },
    content: [
        {text: 'Simple text 1', pageBreak: 'after'},
        {text: 'Simple text 2', pageBreak: 'after'},
        {text: 'Simple text 3', pageBreak: 'after'}
    ]
};

@liborm85
Is there any chance to give distinct bgcolor for each page?

Thanks, @liborm85

Got the solution:

const colors = [ '#CD5C5C', '#F08080', '#008000' ];

this.docDefinition.background = (index) => {
        const canvas = [
                {
                    type: 'rect',
                    x: 0, y: 0, w: 595.28, h: 841.89,
                    color: colors[index - 1]
                }
            ];
            return {canvas};
    };

@StevensonNelli it works great in Firefox/Chrome to alternate bg for different pages, but it fails with a syntax error in IE 11. Have you had any success with getting alternate page backround in IE?

@StevensonNelli worked it out. IE doesn't support " => "
so rewritten the script to be

``` const colors = [ '#007b3d', '#f9c100', '#f08714' ];

  this.dd.background = function (index) {

var canvas = [{
type: 'rect',
x: 0,
y: 0,
w: 595.28,
h: 841.89,
color: colors[index - 1]
}];
return {
canvas: canvas
};
};
```

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jokris1 picture jokris1  路  3Comments

sayjeyhi picture sayjeyhi  路  3Comments

qgliu picture qgliu  路  3Comments

davidyeiser picture davidyeiser  路  3Comments

kumarandena picture kumarandena  路  3Comments