Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text aligned center and continued #774

Open
diegomura opened this issue Feb 3, 2018 · 7 comments
Open

Text aligned center and continued #774

diegomura opened this issue Feb 3, 2018 · 7 comments
Labels

Comments

@diegomura
Copy link
Collaborator

Hi!
In react-pdf we need to render inline texts (for what we use the continued option) that can be also centered in the page. However, when both conditions are present, the render output is not correct. Is there any way to achieve this? Maybe I'm missing something.

We cannot join all strings before render them because there might be a link in the middle of the text string.

screen shot 2018-02-03 at 6 42 54 pm

@liborm85
Copy link
Collaborator

Code to reproduce:

var doc = new PDFDocument();
var stream = doc.pipe(blobStream());

doc.text('Lorem ipsum', {continued: true, align: 'center'});
doc.text('Lorem ipsum', {continued: true, align: 'center'});

doc.end();
stream.on('finish', function() {
  iframe.src = stream.toBlobURL('application/pdf');
});

@liborm85 liborm85 added the bug label Apr 29, 2019
@paul-sanchez
Copy link

Same...

@DeanPoulin
Copy link

I just noticed this myself. I was trying to add an asterisk to the beginning of text with a larger font size than the text itself. When I set the text align center the asterisk ends up in the middle of the sentence.

Sample Code:

doc.fontSize(16)
   .text('* ', {
      align: 'center',
      continued: true
   })
   .fontSize(10)
   .text('This is text centered but the asterisk is in the center instead of beginning');

The result looks like this:

This is text centered but the asterisk*is in the center instead of beginning

Align left and justify work properly.

Align right causes the asterisk to be at the end of the text

Sample Code:

doc.fontSize(16)
   .text('* ', {
      align: 'right',
      continued: true
   })
   .fontSize(10)
   .text('This text is aligned right but the asterisk is at the end instead of beginning');

The result looks like this:

This text is aligned right but the asterisk is at the end instead of beginning*

@EpitechGuo
Copy link

quick fix #1206

@NikolaiMe
Copy link

NikolaiMe commented Sep 2, 2021

Hey all,

I created a package to solve this issue. Maybe it helps some of you!

textbox-for-pdfkit

@jbdemonte
Copy link

issue is still there :(

@ssgabrieldev
Copy link

ssgabrieldev commented Nov 21, 2023

This is my workarownd:

const lines = [
        ['Certifica que ', name],
        ['natural de ', naturality, ', nascido a ', bornDate, ', nacionalidade ', nationality, ','],
        ['portador do Documento de Identificação Nº ', identification, ' válido até ', idValidUntil, ','],
        ['frequentou, de ', start, ' a ', end, ' com a duração total de ', duration, ' horas,'],
        ['o Curso de ', course, '.'],
    ];
    const linesMiddleDivider = [
        4.5,
        2,
        2,
        2,
        2
    ];
    let startY = 265;

    lines.forEach((line, lineIndex) => {
        const lineMiddleDivider = linesMiddleDivider[lineIndex];
        const width = doc.widthOfString(line.join('')) + 2;

        line.forEach((item, itemIndex) => {
            const isVariable = itemIndex > 0 && itemIndex % 2 !== 0;
            let color = '#FFF';

            if (lineIndex === 0 && isVariable) {
                color = '#AD9DF2';
            }

            doc
                .fontSize(12)
                .fillColor(color);

            if (itemIndex === 0) {
                doc.text(item, pageMiddle - (width / lineMiddleDivider), startY, {
                    continued: true,
                    width,
                    align: 'left',
                });

                return;
            }

            if (itemIndex > 0) {
                doc.text(item, {
                    continued: itemIndex !== line.length - 1,
                    align: 'left',
                });
            }
        });

        startY += 20;
    });

output:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants