简体   繁体   English

如何在其中打印带有 canvas 的 div 元素

[英]How to print div element with canvas inside it

I have a page that contains div elements I want to grab and print.我有一个页面,其中包含我想要抓取和打印的 div 元素。 And inside the div there is a canvas that contain a barcode image, and I want the image to be printed as well.在 div 里面有一个包含条形码图像的 canvas,我也希望打印图像。

Here is the page code:这是页面代码:

<div data-v-78bfab32="" class="trx-slip" data-testid="trx-print-slip-2892470852">
<div class="row"> SOME LOGO HERE</div>
    <div data-testid="trx-info-booking-code">
    <hr>
        <div class="row section-logistic-booking">
        <canvas data-testid="trx-barcode" class="barcode" width="166" height="65"></canvas>
            <div>TEXT 1
            </div>
                <div class="c-trx-slip--font-large">TEXT 2
                </div>
                <div class="c-trx-slip--notice-outset">
                    <div class="u-txt--upcase u-txt--bold">TEXT 3 
                    </div>
                </div>
        </div>
    </div>

And this is the preview of that page with CSS:这是带有 CSS 的该页面的预览:

在此处输入图像描述

However, every time I try to grab the ".trx-slip" element, it wouldn't grab the image on the canvas.但是,每次我尝试抓取“.trx-slip”元素时,它都不会抓取 canvas 上的图像。

This is the script i use to grab and print the element:这是我用来抓取和打印元素的脚本:

function printAll(){
var containers = document.querySelectorAll(".trx-slip");
if(!containers){
    alert("Nothing to print");
}
var content = "";
for(var i =0; i < containers.length; i++){
    var addEOL = i + 1 != containers.length;
    content = content + addWrapperDiv(containers[i].outerHTML, addEOL);
}
print(content);
}

function print(content){
var mywindow = window.open('', 'Print', 'height=600,width=800');
mywindow.document.write('<html><head><title>Print</title>');
mywindow.document.write(getCssPart());
mywindow.document.write('</head><body >');
mywindow.document.write(content);
mywindow.document.write('</body></html>');

mywindow.document.close();
mywindow.focus();
mywindow.onload = function(){
mywindow.print();
mywindow.close();
}
}
function addWrapperDiv(content,  addEOL = true){
content = '<div class=".trx-slip">' + content + '</div>';
if(addEOL){
    content = content + '<p style="page-break-after: always" />';
}
return content;
}

function getCssPart(){
var links = document.querySelectorAll("link,style");
var link = "";
for(var i =0; i < links.length; i++){
    link += links[i].outerHTML.replace(/@page[^}]+}/gm,'');
}
return link;
}
printAll();

How to get the image on the canvas to print as well?如何让 canvas 上的图像也打印出来?

I would suggest to replace canvas for img.我建议将 canvas 替换为 img。 With src equals to original dataUrl you will transfer drawing to the preview page and will be able to print it.当 src 等于原始 dataUrl 时,您将把绘图传输到预览页面并能够打印它。

Keep in mind that toDataURL method may throw an exception if drawing is origin-unclean.请记住,如果绘图是原始不干净的,则 toDataURL 方法可能会引发异常。 https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-todataurl https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-todataurl

In your printAll function collect clones of containers and replace canvas with img在您的 printAll function 中收集容器的克隆并用 img 替换 canvas

var containers = [...document.querySelectorAll(".trx-slip")]
                         .map((el)=>{ 
                            var cl = el.cloneNode(true);
                            var cvs = cl.querySelector(".barcode");
                            var dataUrl = cvs.toDataUrl();
                            var img = document.createElement("img");
                            img.src = dataUrl;
                            // set size?
                            cvs.parentElement.replaceChild(img,cvs);
                            return cl;
                         });

then continue with your code.然后继续您的代码。

无法使用<canvas>里面的元素<div>元素</div><div id="text_translate"><p>我无法在 &lt;div&gt; 元素中添加 &lt;canvas&gt; animation 。 使用当前代码,animation 占据整页,我想将 &lt;canvas&gt; 封装在 &lt;div&gt; 中,以便 animation 进入单个部分。 类似于 &lt;div&gt;&lt;canvas&gt; &lt;/canvas&gt;&lt;/div&gt;</p><pre> HTML: &lt;canvas&gt;&lt;/canvas&gt; &lt;h2&gt;hi&lt;/h2&gt;</pre><pre> CSS: * { padding: 0; margin: 0; box-sizing: border-box; } body { width: 100%; height: 100vh; font-family: 'Poppins', sans-serif; background-color: #121212; display: flex; align-items: center; justify-content: center; } canvas { width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 0; } h2 { font-size: 80px; color: white; position: relative; }</pre></div></canvas> - Unable to use <canvas> element inside <div> element

暂无
暂无

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

相关问题 在画布中绘制div元素 - drawing a div element inside a canvas 无法使用<canvas>里面的元素<div>元素</div><div id="text_translate"><p>我无法在 &lt;div&gt; 元素中添加 &lt;canvas&gt; animation 。 使用当前代码,animation 占据整页,我想将 &lt;canvas&gt; 封装在 &lt;div&gt; 中,以便 animation 进入单个部分。 类似于 &lt;div&gt;&lt;canvas&gt; &lt;/canvas&gt;&lt;/div&gt;</p><pre> HTML: &lt;canvas&gt;&lt;/canvas&gt; &lt;h2&gt;hi&lt;/h2&gt;</pre><pre> CSS: * { padding: 0; margin: 0; box-sizing: border-box; } body { width: 100%; height: 100vh; font-family: 'Poppins', sans-serif; background-color: #121212; display: flex; align-items: center; justify-content: center; } canvas { width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 0; } h2 { font-size: 80px; color: white; position: relative; }</pre></div></canvas> - Unable to use <canvas> element inside <div> element JavaScript-如何在div中打印p元素 - JavaScript- how to print p element inside div 如何检测画布内的元素? - how to detect an element inside a canvas? 画布元素更改后如何打印画布元素? - How to print canvas element AFTER canvas element changes? 如何使用它的父div打印html5画布 - How Print html5 canvas with it parent div 如何将div元素放入画布 - how can i put the div element in to canvas 如何在 div 中制作响应式 canvas 元素? - How to make a responsive canvas element within a div? 如何并排对齐 canvas 和 div 元素 - How to align a canvas and a div element side by side 如何在 canvas 中明显居中 div? - How to center div apparently inside canvas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM