Add border arround paragraph with pdfmake

I am generating pdf via pdfmake .

Let's say I have a pdf content like this

var docDefinition = {
    content: [
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a pharetra odio.',
        'Vestibulum erat mauris, sodales et consequat sit amet, ultricies vitae erat. Etiam feugiat orci justo, ultrices malesuada dui ornare ac.',
    ]
};

      

Can I add a border to one of the paragraphs or do I need to use tables for this?

+3


source to share


2 answers


I am not applying borders to the paragraph. I think the only option you have is to use tables.

Below these lines I have attached a simple code that you can paste directly into the pdfmake playground to try it out.



var dd = {
    content: [
        {
            style: 'tableExample',
            color: '#555',
            table: {
                body: [
                    [
                        {
                             text : 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a pharetra odio.\n\nVestibulum erat mauris, sodales et consequat sit amet, ultricies vitae erat. Etiam feugiat orci justo, ultrices malesuada dui ornare ac.'
                        } 
                    ],
                    [
                        {
                             text : 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a pharetra odio.\n\nVestibulum erat mauris, sodales et consequat sit amet, ultricies vitae erat. Etiam feugiat orci justo, ultrices malesuada dui ornare ac.'
                        } 
                    ],
                    [
                        {
                             text : 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a pharetra odio.\n\nVestibulum erat mauris, sodales et consequat sit amet, ultricies vitae erat. Etiam feugiat orci justo, ultrices malesuada dui ornare ac.'
                        } 
                    ],
                ]
            },
            layout: {
                //hLineWidth: function(i, node) {
                //  return (i === 0 || i === node.table.body.length) ? 2 : 1;
                //},
                //vLineWidth: function(i, node) {
                //  return (i === 0 || i === node.table.widths.length) ? 2 : 1;
                //},
                hLineColor: function(i, node) {
                    return (i === 0 || i === node.table.body.length) ? 'red' : 'blue';
                },
                vLineColor: function(i, node) {
                    return (i === 0 || i === node.table.widths.length) ? 'red' : 'blue';
                },
                paddingLeft: function(i, node) { return 40; },
                paddingRight: function(i, node) { return 40; },
                paddingTop: function(i, node) { return 20; },
                paddingBottom: function(i, node) { return 20; }
            }
        }
    ],

    defaultStyle: {
        alignment: 'justify'
    }

}

      

+5


source


Another option is to use a canvas to draw lines such as borders at the position of your text:

{
    canvas: [
        { type: 'line', x1: 390, y1: -80, x2: 510, y2: -80, lineWidth: 1 }, //Up line
        { type: 'line', x1: 390, y1: -35, x2: 510, y2: -35, lineWidth: 1 }, //Bottom line
        { type: 'line', x1: 390, y1: -80, x2: 390, y2: -35, lineWidth: 1 }, //Left line
        { type: 'line', x1: 510, y1: -80, x2: 510, y2: -35, lineWidth: 1 }, //Rigth line
    ]
},

      



This will draw a scraper at the top of the PDF. The caveat is that you can use some sort of silot like: lineWidth

0


source







All Articles