5

我正在使用pdfmake,一个很好的 javascript pdf 打印库在我的 angularjs SPA 中生成 pdf。当我只使用文本时,一切正常。pdf 正确显示所有文本。但是当我尝试显示图像时,它对我不起作用。没有错误,没有空的pdf,什么也没发生。也没有404。

这是我现在正在尝试的代码,

var dd = {
        content: [
            {
                image: 'picture13438908025659.jpg'
            },
            {
                text: 'Patient\'s Detail', fontSize: 18, bold: true
            },
            {
                columns: [
                    {
                        text: 'Patient\'s ID: ' + $scope.prescription.Patient[0].id, bold: false
                    },
                    {
                        text: 'Name: ' + $scope.prescription.Patient[0].name, bold: false
                    },
                    {
                        text: 'Age: ' + $scope.prescription.Patient[0].age, bold: false
                    },
                    {
                        text: 'Contact: ' + $scope.prescription.Patient[0].contact, bold: false
                    }
                ]

            },
            {
                text: 'Address: ' + $scope.prescription.Patient[0].address, bold: false
            }
        ]
    }
4

4 回答 4

5

这很简单,

1)在这里将您的图像转换为base64

2)复制您的base64代码并替换为,

image : 'base64', // use your base64 instead image.jpg

3) 完成。这种方法是如果您的图像不是通过链接工作的,则使用 base64 代替

于 2016-04-09T09:31:03.427 回答
2

您可以轻松地将任何图像转换为 base64 格式,根据文档,这是首选格式。这是一个 100% 的工作代码:

function convertImgToBase64URL(url, callback, outputFormat){
    var canvas = document.createElement('CANVAS'),
    ctx = canvas.getContext('2d'),
    img = new Image;
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var dataURL;
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        callback(dataURL);
        canvas = null; 
    };
img.src = url;
}

在此之后,您只需在您的 pdf 创建代码中提及它

var docDefinition = {
  content: [
    {
      // you'll most often use dataURI images on the browser side
      // if no width/height/fit is provided, the original size will be used
      image: 'data:image/jpeg;base64,...encodedContent...'
    }]
于 2015-05-04T13:07:30.490 回答
1

您首先需要将您的图像转换为base64,然后您可以在图像属性中使用它,像这样

{
  image: 'data:image/jpeg;base64,...encodedContent...',
  width: 75,
  height: 75,
  alignment : 'left'
}

这是一个参考 - https://pdfmake.github.io/docs/0.1/document-definition-object/images/

于 2021-01-02T15:21:17.473 回答
0

您可以添加width:your_required_width图像对象并检查它是否有效吗?它也发生在我身上,我发现图像分辨率太大,PDFMake 只是以原始尺寸显示图像,不适合标准尺寸的纸张。为了将来使用PDFMake 游乐场创建您的文档定义对象,它非常方便。

于 2015-08-16T17:09:36.787 回答