html - 如何在浏览器中跨多个页面宽度打印大 Canvas ?

标签 html css canvas browser

我的应用程序需要打印出可以跨越多个页面宽度和高度宽度的任意大 Canvas 。

前段时间有一个类似的问题where it was claimed the browser won't print to multiple page widths .

因为这是一段时间前的事了,我想知道它是否仍然是真的。另外,有什么策略可以在不拆分的情况下打印出大 Canvas ?

var canvas = document.getElementById("canvas1");

function draw_a() {
  var context = canvas.getContext("2d");
  //   //  LEVER

  //plane
  context.fillStyle = '#aaa';
  context.fillRect(25, 90, 2500, 400);


}

$(document).ready(function() {
  draw_a();

});
canvas {
  border: 1px dotted;
}

.printOnly {
  display: none;
}

@media print {
  html,
  body {
    height: 100%;
    background-color: yellow;
  }
  .myDivToPrint {
    background-color: yellow;
    /*
        height: 100%;
    
        width: 100%;
        position: fixed;*/
    top: 0;
    left: 0;
    margin: 0;
  }
  .no-print,
  .no-print * {
    display: none !important;
  }
  .printOnly {
    display: block;
  }
}

@media print and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
  html,
  body {
    height: 100%;
    background-color: yellow;
  }
  .myDivToPrint {
    background-color: yellow;
    /*
        height: 100%;
    
        width: 100%;
        position: fixed;*/
    top: 0;
    left: 0;
    margin: 0;
    padding: 15px;
    font-size: 14px;
    line-height: 18px;
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    transform: rotate(90deg);
  }
  .no-print,
  .no-print * {
    display: none !important;
  }
  .printOnly {
    display: block;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="window.print();" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
  <div class="Aligner-item">
    <canvas height="2500px" width="4000px" id="canvas1"></canvas>
    <div class="printOnly Aligner-item--bottom"> Print Only</div>
  </div>

</div>

最佳答案

浏览器似乎会将大 Canvas 分成多个页面。我使用最新的 chrome 和 safari 浏览器在 MacOS Sierra 上进行了测试。

打印 Canvas 的一种可能方法是首先将其转换为 data URI包含使用 canvas.toDataURL() 表示的图像。然后,您可以在打印之前操纵图像尺寸。

"<img src='" + canvas.toDataURL() + "' height='500px' width='500px' />'"

在下面的例子中,4500px canvas 的大4500px 被翻译成一个img 并放在里面iframe,用于打印。您可以将图像附加到原始文档,而不是打印该特定元素,但 iframe 可能更灵活地处理打印输出。您可以根据需要操作 img 尺寸并打印 Canvas 的缩放表示。请注意,我对图像的 widthheight 进行了硬编码,但这可以根据打印需要进行计算和更改。

由于 iframe 跨源限制,下面的代码片段在这里不起作用,但它在 this jsfiddle 上有效。 .

代表 Canvas 的缩放 500px x 500px 图像在打印时适合一页。

var canvas = document.getElementById("canvas1");

function draw_a() {
  var context = canvas.getContext("2d");
  //   //  LEVER

  //plane
  context.fillStyle = '#aaa';
  context.fillRect(25, 90, 4500, 4500);
}

print = function() {
	window.frames["myFrame"].focus();
	window.frames["myFrame"].print();
}

function setupPrintFrame() {
	$('<iframe id="myFrame" name="myFrame">').appendTo("body").ready(function(){
    setTimeout(function(){
        $('#myFrame').contents().find('body').append("<img src='" + canvas.toDataURL() + "' height='500px' width='500px' />'");
    },50);
	});
}

$(document).ready(function() {
  draw_a();
	setupPrintFrame();
});
canvas {
  border: 1px dotted;
}

.printOnly, #myFrame {
  display: none;
}

@media print {
  html,
  body {
    height: 100%;
    background-color: yellow;
  }
  .myDivToPrint {
    background-color: yellow;
    /*
        height: 100%;
    
        width: 100%;
        position: fixed;*/
    top: 0;
    left: 0;
    margin: 0;
  }
  .no-print,
  .no-print * {
    display: none !important;
  }
  .printOnly {
    display: block;
  }
}

@media print and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
  html,
  body {
    height: 100%;
    background-color: yellow;
  }
  .myDivToPrint {
    background-color: yellow;
    /*
        height: 100%;
    
        width: 100%;
        position: fixed;*/
    top: 0;
    left: 0;
    margin: 0;
    padding: 15px;
    font-size: 14px;
    line-height: 18px;
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    transform: rotate(90deg);
  }
  .no-print,
  .no-print * {
    display: none !important;
  }
  .printOnly {
    display: block;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button onclick="print()" class="no-print">Print Canvas</button>
<div class="myDivToPrint">
  <div class="Aligner-item">
    <canvas height="4500px" width="4500px" id="canvas1"></canvas>
    <div class="printOnly Aligner-item--bottom"> Print Only</div>
  </div>

</div>

关于html - 如何在浏览器中跨多个页面宽度打印大 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007342/

相关文章:

jquery - 使用 jQuery 在元素末尾添加 "more"html?

javascript - 对象 #<HTMLLIElement> 没有方法 'bind'

css - 字体不显示

javascript - 如何将字符串一次转换为数学函数?

javascript - 如何读取文本框中的值并添加一些扩展名

html - 调整 mat-raised-button 的大小

html - 多级嵌套 Bootstrap 结构布局建议?

javascript - 通过 JavaScript 插入 HTML

javascript - 为什么这个 Canvas 代码这么慢?

javascript - 是否可以在上面有一个带有可滚动内容区域的自动高度 div 页脚?