Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFKit - Text over rectangle

Is it posible to render text over rectangle using PDFKit. Maybe it is possible to use hack, to have fill of the rectangle with opacity - but I don't want to use it that way. My text is hidden by rectangle (I am creating table by alternating rectangles with different colors).

UPDATE

I figured out that text is somehow same color as rectangles, that is probably why I don't see it. But why ?

 var doc = new PDFDocument({
    size: 'A4',
    margin: 25
  });
  doc.fontSize(11);
  doc.lineWidth(0.5);

  const projects = Projects.find().fetch();

  const rectXOffset = 25;
  const rectYOffset = 25;
  let rectPosition = 25;

  let counter = 0;

  for (var project of projects) {


    if (counter % 2 == 0)
    {

     doc.rect(rectXOffset, rectPosition, doc.page.width - rectXOffset * 2, 25).fill("#ddd");

    }
    else
    {

      doc.rect(rectXOffset, rectPosition, doc.page.width - rectXOffset * 2, 25).fill("#c9c9c9");
    }

    rectPosition += rectYOffset;
    counter++;

    doc.text(project.projectName,100,100).fillColor("red");

  }


  doc.write(process.env.PWD + '/PDFKitExampleServerSide.pdf');
like image 276
Sysrq147 Avatar asked Jul 21 '26 22:07

Sysrq147


1 Answers

You need to use the fill and / or stroke methods to actually draw the rectangle, then redefine the colors for the text to come. This works for me to draw red text in a grey box that has a black border:

    doc.rect(45, 165, 240, 22).fillAndStroke('#ddd', '#000');
    doc.fill('#F00').stroke();
    doc.fontSize(16);
    doc.text("Sample text", 50, 170, {lineBreak: false} );

This is because doc.text doesn't pass a color, so it uses the old one.

like image 136
Guntram Blohm Avatar answered Jul 23 '26 10:07

Guntram Blohm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!