Pdfkit: Inline images

Created on 1 Jul 2017  路  2Comments  路  Source: foliojs/pdfkit

Any hints on doing this?

Most helpful comment

For the record, I found a solution, but it's hacky, since it relies on undocumented stuffs.
This code injects an image in the middle of inline text:

var image = pdfDoc.openImage( imagePath ) ;

var size = {
  width: image.width ,
  height: image.height
} ;

var position = {
  x: pdfDoc.x ,
  y: pdfDoc.y
} ;

// If there is a _wrapper property, we are in the middle of 'continued' text
if ( pdfDoc._wrapper ) { position.x += pdfDoc._wrapper.continuedX ; }

// Backup 'y'
var yBackup = pdfDoc.y ;

// Put the image
pdfDoc.image( image , position.x , position.y , size ) ;

// If there is a wrapper, don't forget to update its 'continuedX' value
if ( pdfDoc._wrapper ) { pdfDoc._wrapper.continuedX += size.width ; }

// Restore 'y'
pdfDoc.y = yBackup ;

It works but it needs improvements: it doesn't move the image to the next line if there isn't enough room. But that's the way to go.

I hope this will help someone...

All 2 comments

It looks like pdfDoc.x is not updated when using continued text, so I don't know how to achieve this as well...
Any idea?

@jonasbarsten Have you found something?

For the record, I found a solution, but it's hacky, since it relies on undocumented stuffs.
This code injects an image in the middle of inline text:

var image = pdfDoc.openImage( imagePath ) ;

var size = {
  width: image.width ,
  height: image.height
} ;

var position = {
  x: pdfDoc.x ,
  y: pdfDoc.y
} ;

// If there is a _wrapper property, we are in the middle of 'continued' text
if ( pdfDoc._wrapper ) { position.x += pdfDoc._wrapper.continuedX ; }

// Backup 'y'
var yBackup = pdfDoc.y ;

// Put the image
pdfDoc.image( image , position.x , position.y , size ) ;

// If there is a wrapper, don't forget to update its 'continuedX' value
if ( pdfDoc._wrapper ) { pdfDoc._wrapper.continuedX += size.width ; }

// Restore 'y'
pdfDoc.y = yBackup ;

It works but it needs improvements: it doesn't move the image to the next line if there isn't enough room. But that's the way to go.

I hope this will help someone...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tadarao picture tadarao  路  5Comments

stephen-last picture stephen-last  路  4Comments

alFReD-NSH picture alFReD-NSH  路  4Comments

ehaubold picture ehaubold  路  5Comments

michapixel picture michapixel  路  4Comments