简体   繁体   English

根据图像的宽度和高度设置pdf宽度和高度

[英]set pdf width and height according to image width and height

I want to set the PDF width and height equal to the image width and height. 我想将PDF的宽度和高度设置为等于图像的宽度和高度。 But the image width and height is coming as 0. 但是图像的宽度和高度为0。

I also used image.onload() but that is not getting called. 我也使用了image.onload()但是没有被调用。

This is HTML: 这是HTML:

<span class="col-sm-3 padding0">
    <button id="downloadModel" class="btn btn-md btn-default"
            style="height:100px;width: 210px" 
            ng-click="downloadWorkModel()">
          Download Work Model
    </button>
</span>

CODE: 码:

$scope.downloadWorkModel= function(){
  var image = new Image();
  image.src = 'data:image/png;base64,'+$scope.workData.image;
  document.body.appendChild(image);
  console.log("image width: "+image.width); //coming 0
  console.log("image height: "+image.height); //coming 0
  var pdf = new jsPDF("l", "mm");
  pdf.addImage(image, 'png', 0, 0);
  pdf.save('workModel.pdf');
};

NOTE: 注意:

This doesn't work either (it doesn't goes inside onload)- 这也不起作用(它也不在onload内)-

$scope.downloadWorkModel= function(){
    var image = new Image();
    var width, height;
    image.onload = function() {
        width= this.width;
        height= this.height;
    };
    image.src = 'data:image/png;base64,'+$scope.workData.image;
    document.body.appendChild(image);
    console.log("image width: "+image.width); //coming 0
    console.log("image height: "+image.height); //coming 0
    var pdf = new jsPDF("l", "mm", [width, height]);
    pdf.addImage(image, 'png', 0, 0);
    pdf.save('workModel.pdf');
};

@mplungjan- @ mplungjan-

After putting your code the download pdf content becomes too small to view- 输入代码后,下载的pdf内容变得太小而无法查看-

在此处输入图片说明

Try this. 尝试这个。 The load is async enough to not show the size until after the load 加载是异步的,直到加载后才显示大小

$scope.downloadWorkModel = function() {
  var image = new Image();
  var width, height;
  image.onload = function() {
    width = this.width;
    height = this.height;
    document.body.appendChild(this);
    console.log("image width: " + width);
    console.log("image height: " +height);
    var pdf = new jsPDF("l", "mm", [width, height]);
    pdf.addImage(this, 'png', 0, 0);
    if (done) {  // something needs to decide when this is true
      pdf.save('workModel.pdf'); 
    }
  };
  image.src = 'data:image/png;base64,' + $scope.workData.image;
};
$scope.downloadWorkModel= function(){
      let image = new Image();
      let width, height;
      image.src = 'data:image/png;base64,'+$scope.workData.image;
      let addedImage = document.body.appendChild(image);
      width = addedImage.width;
      height = addedImage.height;
      console.log("image width: "+width); //coming 0
      console.log("image height: "+height); //coming 0
      var pdf = new jsPDF("l", "mm", [width, height]);
      pdf.addImage(image, 'png', 0, 0);
      pdf.save('workModel.pdf');
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM