Search code examples
javascriptjspdf

What is the exact list of parameters and their description for jspdf's addImage() function?


I can see in the console that addImage() function of jspdf library has 9 parameters. I've seen people using first 5 of them, but not completely sure even about them. Unfortunately, documentation for this method is not provided on official web-site. Also, corresponding github issues are closed.


Solution

  • From the source you can find the method signature as follows:

    function(imageData, format, x, y, w, h[, alias[, compression[, rotation]]])
    

    imageData has to be:

    • an ImageData object
    • an <img> element
    • a <canvas> element
    • an image as a data uri
    • or an object with at least an imageData property (with one of the above data types)
      In this case the object can then also define the values of the other parameters as properties of the object.

    format has to be:

    • one of the following strings (case insensitive): 'jpeg', 'jpg', 'png'
    • If the type is not string then the script acts like the function has the following signature (format is now the sixth parameter):

      function(imageData, x, y, w, h[, format[, alias[, compression[, rotation]]]])
      

      where format would have a default value of jpeg

    x and y have to be:

    • numbers which define the upper left corner of the image in the PDF (in millimeters!)

    w and h have to be:

    • numbers which define the width and height of the image (also in millimeters)

    alias (optional) has to be:

    • a unique string to identify the image (used for the internal cache)
    • or undefined or null In this case the unique identifier is a hash of the image

    compression (optional) has to be:

    • one of the following strings (case insensitive): 'NONE', 'FAST', 'MEDIUM', 'SLOW' Any other type or value will set the compression to NONE

    rotation (optional, only relevant when imageData is a DOM element) has to be:

    • a number defining the rotation in degrees
    • or an object which can then define:
      • the angle of rotation (angle)
      • the center of rotation (x and y) (optional, default: center of the canvas)
      • the background color (bg) (optional, default: 'white')

    The answer is based on the code available here.
    If there are any errors or missing information feel free to add them :)