Pdfkit: Missing Text

Created on 15 Feb 2018  路  8Comments  路  Source: foliojs/pdfkit

We have a bug that seems fairly random. Occasionally some of the text we pass into the .text() will just not show up.

We are running
Node: V8.9.1
NPM: V5.6.0
Ubuntu: 16.04x64
pdfkit 0.8.3

.text(record.term10.replace(/\r/g, ''),  { align: 'justify'})
.font('Times-Roman')
.fontSize(11)
.text(record.term18.replace(/\r/g, ''), {align: 'justify' })
.text(record.term25.replace(/\r/g, ''), {align: 'justify' })
.pipe(fs.createWriteStream(name))
 doc.end()

In the code above where we have record.term25 part of the text is missing.

The text segment is about 8,000 characters.

The missing text comes near the top

  1. MARKETING: You authorize us to share your contact information and information about this lease. Please call us at 1-xxx-xxx-xxxx if you prefer we did not share your contact and lease information.

  2. CREDIT INQUIRIES

In the above lines (edited to remove the phone number) the following line does not show up

Please call us at 1-xxx-xxx-xxxx if you prefer we did not share your contact and lease information.

To get the line to show up we have to put it twice as shown below

  1. MARKETING: You authorize us to share your contact information and information about this lease. Please call us at 1-xxx-xxx-xxxx if you prefer we did not share your contact and lease information. Please call us at 1-xxx-xxx-xxxx if you prefer we did not share your contact and lease information.

  2. CREDIT INQUIRIES

Once we do this everything comes up correctly and it shows the line.

We had this bug in one other place earlier, however, due to edits we made in the format that error vanished.

If anyone needs the full code, input, and pdf generated with this to help troubleshoot I would be happy to send it to you.

Most helpful comment

Was able to work around the issue using the following code:

                let text = doc.text('', 50, 50).font('Courier', 10)
                msg.text.split('\n').forEach((line)=>{
                    line = (line === '') ? ' ' : line
                    text = text.text(line)
                })

All 8 comments

I found the similar issue. For now, what I've done is to split the characters line by line and save it to Array. Then, Looping through the array and displaying it solves the problem for now. But would be happy if we get proper solution

Possibly the same issue. The first line of text doesn't show in some cases:

pdfkit 0.8.3
MacOS 10.13.4
node 8.9.1

var PDFDocument = require("pdfkit");
var doc = new PDFDocument();
var notes = 'with this certificate acknowledging the completion of Ladder Safety 1 on this 24th day of May 2018.\n\n1 retry on 2 quiz questions (retried question 2).';
doc.font('Times-Roman');
doc.fontSize(13).text(notes, 50, 308, { width: 514 });
doc.end();

pdfkit-bug

Having the same issue and have verified it's not a special character issue using the following code. We have had to extensively deal with special characters as we are pulling reports out of a VT100 terminal system. We though we were good and have been running this in production for close to a year now. Recently one of our radiologist noticed the issue.

Centos 7.3
nodejs: 6.14.0
pdfkit: 0.8.3

            // debugging
            let next = 0
            this.report.split('').forEach((c)=>{
                let code = c.charCodeAt(0)
                if (((code < 32 || code > 126) && code !== 10) || next > 0) {
                    if (code === 27) {
                        next = 5
                    }
                    next--
                    console.log(`${c} = ${c.charCodeAt(0)}`)
                }
            })
            // end debugging

Code from our microservice that gets called to creates the pdf.

        this.add(`role:${ROLE},cmd:createPdfFromTxt`, function product(msg, done) {
            const doc = new PDFDocument()
            const filename = `${uuidv4()}.pdf`
            const file = msg.file || `${folder}/${filename}`

            let fsStream = fs.createWriteStream(file)
            fsStream.on('error', function(err) {
                done(new Error(err))
                console.error(err)
            }).on('finish', function(arg) {
                done(null, {result: `${file}`})
            })
            doc.pipe(fsStream)
            doc.y = 300
            doc.text(text, 50, 50)
            doc.end()
        })

Raw text after being cleaned of special characters:
pdfkitbug1

PDF
pdfkitbug2

Was able to work around the issue using the following code:

                let text = doc.text('', 50, 50).font('Courier', 10)
                msg.text.split('\n').forEach((line)=>{
                    line = (line === '') ? ' ' : line
                    text = text.text(line)
                })

I found the same issue, would you share the solution with me ? thanks

thanks @MichaelLeeHobbs ,but I still don`t understand the reason in it, because of '\n' ?

This might be related to this fix: https://github.com/devongovett/pdfkit/pull/764 (which is merged but not released to NPM). This issue relates to improper text wrap handling.

We had to manually alter our node_modules/pdfkit/lib/line_wrapper.js to reflect the change

FWIW - I upgraded from 0.8.3 to 0.9.0, and it's fixed for me.

0.9.0 was released on NPM Jan 27, 2019.

Was this page helpful?
0 / 5 - 0 ratings