我正在使用pdfmake来创建PDF,并成功使用了数据URI来嵌入图像。这是一个很小的图像,宽约200像素,我想将其右对齐。有什么方法可以将图像推送到PDF的右侧吗?

最佳答案

您可以使用文档定义中的预定义样式对图像进行右对齐。 pdfmake游乐场有一个很好的图像示例,我对其进行了编辑,以在下面添加“右对齐”样式(称为“rightme”)。我尝试仅在文档定义中直接添加“alignment:right”,但这不起作用。
由于长度,我删除了图像数据-请访问pdfmake playground图像以查看其工作原理:

  var dd = {
    content: [
      'pdfmake (since it\'s based on pdfkit) supports JPEG and PNG format',
      'If no width/height/fit is provided, image original size will be used',
      {
        image: 'sampleImage.jpg',
      },
      'If you specify width, image will scale proportionally',
      {
        image: 'sampleImage.jpg',
        width: 150
      },
      'If you specify both width and height - image will be stretched',
      {
        image: 'sampleImage.jpg',
        width: 150,
        height: 150,
      },
      'You can also fit the image inside a rectangle',
      {
        image: 'sampleImage.jpg',
        fit: [100, 100],
        pageBreak: 'after'
      },

      // Warning! Make sure to copy this definition and paste it to an
      // external text editor, as the online AceEditor has some troubles
      // with long dataUrl lines and the following image values look like
      // they're empty.
      'Images can be also provided in dataURL format...',
      {
            image: **'IMAGE TRUNCATED FOR BREVITY'**,
            width: 200,
            style: 'rightme'
      },
      'or be declared in an "images" dictionary and referenced by name',
      {
        image: 'building',
        width: 200
      },
    ],
    images: {
      building: **'IMAGE DATA TRUNCATED FOR BREVITY'**
    },
    styles:{
        rightme:
        {
            alignment: 'right'
        }

    }

  }

10-06 11:27