当前位置: 首页 > 知识库问答 >
问题:

如何在下载的pdf中使用html2画布捕获徽标和当前日期

柴华灿
2023-03-14

我正在使用jspdf打印和下载pdf中div的内容。这里也是在pdf中,我需要捕捉页面左上角的图像和页面右上角的当前日期作为我的html。可能是它需要使用的位置和附加在html2画布。我是新来的html2画布,谁能帮我请。这是密码

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.min.js"></script>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="col-md-12">
<div class="pull-left" id = "logo"><img src="logo.png" title="logo"></div>
<div class="pull-right" id = "date"></div>
</div>
<div id="content">
      <p>This is the element you only want to capture</p>   
       <p>This is the element you only want to capture</p>  
        <p>This is the element you only want to capture</p> 
         <p>This is the element you only want to capture</p>    
          <p>This is the element you only want to capture</p>   
          <p>This is the element you only want to capture</p>   
       <p>This is the element you only want to capture</p>  
        <p>This is the element you only want to capture</p> 
         <p>This is the element you only want to capture</p>    
          <p>This is the element you only want to capture</p>   
    </div>

    <button id="print">Download Pdf</button>
    <footer>This is the footer</footer>
$(document).ready(function() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd = '0'+dd
} 

if(mm<10) {
    mm = '0'+mm
} 

today = mm + '/' + dd + '/' + yyyy;
$('#date').append(today);
      $('#print').click(function() {
     var currentPosition = document.getElementById("content").scrollTop;
      var w = document.getElementById("content").offsetWidth;
      var h = document.getElementById("content").offsetHeight;
     document.getElementById("content").style.height="auto";

      html2canvas(document.getElementById("content"), {

        dpi: 300, // Set to 300 DPI
        scale: 3, // Adjusts your resolution
        onrendered: function(canvas) {
          var img = canvas.toDataURL("image/jpeg", 1);
          var doc = new jsPDF('L', 'px', [w, h]);
          doc.addImage(img, 'JPEG', 0, 0, w, h);
          doc.addPage();
          doc.save('sample-file.pdf');
        }
      });
     document.getElementById("content").style.height="100px";
     document.getElementById("content").scrollTop = currentPosition;
    });

    });
body {
      background: beige;
    }

    header {
      background: red;
    }

    footer {
      background: blue;
    }

    #content {
      background: yellow;
      width: 70%;
      height: 100px;
      margin: 50px auto;
      border: 1px solid orange;
      padding: 20px;
      overflow-y:auto;
    }
    .html2canvas-container { width: 3000px !important; height: 3000px !important; }container { width: 3000px !important; height: 5000px !important; }

共有1个答案

廉雅惠
2023-03-14

尝试转换您的图像在Base64

var src = 'data:image/png;base64,iVBORw0K.......'
//doc.addImage(src, 'PNG', x, y, w, h);
doc.addImage(src, 'PNG', 0, 240, 210, 57);

https://jsfiddle.net/q8u9jhro/2/

 类似资料:
  • 问题内容: 如何在JavaScript中获取当前日期? 问题答案: 使用生成一个新的包含当前日期和时间对象。 这将以mm / dd / yyyy的格式给您今天的日期。 只需更改为所需的任何格式。

  • 问题内容: 是否可以在手动查询中使用诸如DATETIME之类的值或命令来插入当前日期和时间? 末尾引用的DATETIME值是我要添加当前日期和时间的位置。 问题答案: 您可以使用:

  • 问题内容: 如何使用Java获取当前日期和时间? 我正在寻找与C#等效的东西。 问题答案: 只需构造一个没有任何参数的新对象即可;这会将当前日期和时间分配给新对象。 在的话的Javadoc的无参数的构造函数: 分配一个Date对象并对其进行 初始化,以便它表示分配它的时间( 以最近的毫秒为单位)。 确保您使用的不是-后者没有零参数构造函数,并且语义有所不同,这是完全不同的对话的主题。:)

  • 问题内容: 当我创建一个新Date对象时,它将被初始化为当前时间,但是在本地时区。如何获取格林尼治标准时间的当前日期和时间? 问题答案: 尽管它的值通常是与UTC有关的,但它没有特定的时区。是什么让你认为这是当地时间? 准确地说:内的值是自Unix时代(UTC)在1970年1月1日午夜开始的毫秒数。相同的纪元也可以在其他时区进行描述,但是传统的描述是以UTC表示的。由于距固定纪元以来的毫秒数,因此

  • 我正在实现一个ASP api来完成所有Docusign api流(获取登录信息,获取令牌,发送要签名的信封,下载信封文档)。现在我试图在浏览器中显示一个下载的文档,该文档是空白的。我正在从docusign接收文档字节内容,并试图将其呈现在iframe中。 docusign发送的PDF内容如下 更新 如果文档超过1页,则可以看到所有页面,全部为空白······

  • 问题内容: 我正在尝试使用selenium从网站下载pdf文件,但我能够打开文件,但无法使用代码自动下载。 码: 请提出建议。先感谢您 问题答案: 以上问题现已解决