首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印输出WaterMark页面上的html

打印输出WaterMark页面上的html
EN

Stack Overflow用户
提问于 2016-04-07 15:49:23
回答 2查看 3.6K关注 0票数 1

https://jsfiddle.net/dhaileytaha/g92gr1sy/3/

上面的小提琴有一个打印输出页面。我需要在打印页面上有一个WaterMark。我不知道我们该怎么做。无论是js、jquery还是css,或者以上都不是,请帮助

代码语言:javascript
复制
 document.getElementById("btnPrint").onclick = function() {
 var password;

 var pass1 = "cool";

 password = prompt('Please enter your password to view this page!', ' ');

 if (password == pass1) {
   printElement(document.getElementById("example"));
   //console.log(elem);
  }
 }
   //console.log(elem);
   function printElement(elem) {
     var domClone = elem.cloneNode(true);
     console.log(elem);
     var $printSection = document.getElementById("printSection");

   if (!$printSection) {
     $printSection = document.createElement("div");
     $printSection.id = "printSection";
     document.body.appendChild($printSection);
   }

    $printSection.innerHTML = "";
    $printSection.appendChild(domClone);
    window.print();
   }
EN

回答 2

Stack Overflow用户

发布于 2016-04-07 15:58:56

我相信"media query: Print"可能会对您有所帮助。它与所有其他媒体查询基本相同(如“屏幕”和“投影”等)

您可以生成元素(水印)并仅在打印模式下显示它:

代码语言:javascript
复制
@media print { 
 /* All your print styles go here */
 #header, #footer, #nav { display: none !important; } 
}

你可以参考this detailed explanation

票数 0
EN

Stack Overflow用户

发布于 2017-04-27 16:33:24

如果服务器可以提供水印图像,并且您的所有用户都在使用webkit浏览器,请尝试

代码语言:javascript
复制
@media print {
 * {
       -webkit-print-color-adjust: exact;
   }
}

container.style.backgroundImage = 'url(picture-link)'

如果没有,试试这个

代码语言:javascript
复制
container.style.position = 'relative'

  // watermarkElm is a dom node
  if (watermarkElm) {
    const background = document.createElement('section')

    const baseStyle = {
      position: 'absolute',
      top: 0,
      left: 0,
      bottom: 0,
      right: 0,
      height: '120%',
      width: '120%',
      overflow: 'hidden',
      wordWrap: 'break-word',
      wordBreak: 'normal',
      zIndex: 9999,
      pointerEvents: 'none'
    }

    Object.assign(background.style, baseStyle)
    container.appendChild(background)

    // watermarkCount 
    renderWatermark(watermarkElm.outerHTML, watermarkCount, background)

function renderWatermark (tpl, watermarkCount, background) {
  if (!tpl) {
    throw new Error('invalid watermark template')
  }

  background.innerHTML = new Array(watermarkCount).fill(tpl).join('')
  return background
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36469871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档