如何使用全日历插件实现打印功能



我正在使用完整日历作为演示。 如何实现打印功能?我在FullCalendar包中找到了一个文件'fullcalendar.print.css',它有什么用?

如果有一个按钮"打印",点击按钮时我会怎么做?任何帮助将不胜感激!

我知道

这是一个有点老的问题,但我想我还是会回答。

以文件告诉您的方式包含文件,例如:

<link href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/1.6.4/fullcalendar.css" rel="stylesheet" type="text/css" />
<link href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/1.6.4/fullcalendar.print.css " rel="stylesheet" type="text/css" media="print" />

然后,我将按如下方式设置我的按钮:

<button class="printBtn hidden-print">Print</button>
<script type="text/javascript">
  $('.printBtn').on('click', function (){
    window.print();
  });
</script>

这样,您的按钮就会从打印屏幕上隐藏,当您单击该按钮时,它会为您弹出一个打印屏幕。

隐藏打印是 Bootstrap 提供的一个类,如果您不使用引导程序,则可以在样式表中使用如下内容:

@media print {
  .visible-print  { display: inherit !important; }
  .hidden-print   { display: none !important; }
}

至于使用fullcalendar.print.css? 当您media == print时,它会根据您的<link>

应用

您可以使用以下链接中使用的解决方案来制定Primefaces时间表(与完整日历相同)。http://eves4code.blogspot.in/2014/12/to-print-primefaces-schedule.html

本示例在新标签页中打开日历的打印预览。

从上面的链接中提取

的是

此示例是为在后端使用完整日历的素数计划组件实现的

printScheduleArea 是包含素数计划表的div 标签。

在 XHTML 中,为新打印添加链接标记.css

<link rel="stylesheet" href="#{request.contextPath}/css/print.css" 
type="text/css" media="print"/>

印刷中.css

@media print {
    body, html, #wrapper {
     width: 100%;
    }
   div {
     overflow: visible !important;      
    }
}

在视图页面中添加打印按钮。

function printPreview() {
  var headerElements = document.getElementsByClassName('fc-header');//.style.display = 'none';
  for(var i = 0, length = headerElements.length; i < length; i++) {
    headerElements[i].style.display = 'none';
  }
  var toPrint = document.getElementById('printScheduleArea').cloneNode(true);
  for(var i = 0, length = headerElements.length; i < length; i++) {
        headerElements[i].style.display = '';
  }
  var linkElements = document.getElementsByTagName('link');
  var link = '';
  for(var i = 0, length = linkElements.length; i < length; i++) {
    link = link + linkElements[i].outerHTML;
  }
  var styleElements = document.getElementsByTagName('style');
  var styles = '';
  for(var i = 0, length = styleElements.length; i < length; i++) {
    styles = styles + styleElements[i].innerHTML;
   }
  var popupWin = window.open('', '_blank');
  popupWin.document.open();
  popupWin.document.write('<html><title>Schedule Preview</title>'+link
 +'<style>'+styles+'</style></head><body">')
  popupWin.document.write(toPrint.innerHTML);
  popupWin.document.write('</html>');
  popupWin.document.close();
  setTimeout(popupWin.print(), 20000);
}

必需的 js 文件

<script  src="https://cdnjs.cloudflare.com/ajax/libs/amcharts/3.13.3/exporting/jspdf.plugin.addimage.js"></script>
<script  src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.5.0-beta4/html2canvas.min.js"></script>
   
  /*print*/
    <script>
        $("#print_calendar").click(function () {
        
            html2canvas(document.getElementById("calendar")).then(function (canvas) {
            
            //for give white BG
            var context  = canvas.getContext('2d');
            context.save();
            context.globalCompositeOperation = 'destination-over';
            context.fillStyle = "rgb(255, 255, 255)";
            context.fillRect(0, 0, canvas.width, canvas.height);
            context.restore();
            
            var imgData = canvas.toDataURL('image/jpeg',1);
            
            //for Save as Image
            /*
            downloadImage(imgData, 'my-canvas.jpeg');
            return; 
            */
            //var doc = new jsPDF();
            var doc = new jsPDF("p", "mm", "a4");
            doc.addImage(imgData, 'JPEG', 10, 10,180, 297);
            download(doc.output(), "calendar.pdf", "text/pdf");
            
        });
    });
    // Save As Image
    function downloadImage(data, filename = 'untitled.jpeg') {
        var a = document.createElement('a');
        a.href = data;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
    }
    
    //Save As PDF (Using jsPDF)
    function download(strData, strFileName, strMimeType) {
        var D = document,
            A = arguments,
            a = D.createElement("a"),
            d = A[0],
            n = A[1],
            t = A[2] || "text/plain";
        //build download link:
        a.href = "data:" + strMimeType + "," + escape(strData);
        if (window.MSBlobBuilder) {
            var bb = new MSBlobBuilder();
            bb.append(strData);
            return navigator.msSaveBlob(bb, strFileName);
        } /* end if(window.MSBlobBuilder) */
        if ('download' in a) {
            a.setAttribute("download", n);
            a.innerHTML = "downloading...";
            D.body.appendChild(a);
            setTimeout(function() {
                var e = D.createEvent("MouseEvents");
                e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false,
                    false, false, 0, null);
                a.dispatchEvent(e);
                D.body.removeChild(a);
            }, 66);
            return true;
        } /* end if('download' in a) */
        //do iframe dataURL download:
        var f = D.createElement("iframe");
        D.body.appendChild(f);
        f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64"
            : "") + "," + (window.btoa ? window.btoa : escape)(strData);
        setTimeout(function() {
            D.body.removeChild(f);
        }, 333);
        return true;
    } 
    </script>
    /*print*/

这是你的HTML代码

<div class="" id="component1">
      <full-calendar></full-calendar>
    </div>

添加打印按钮并将 ID 传递给方法

<button (click)="printComponent('component1')">Print</button>

--

printComponent(cmpName) {
        var docHead = document.head.outerHTML;
        var printContents = document.getElementById('component1').outerHTML;
        var winAttr = "location=yes, statusbar=no, menubar=no, titlebar=no, toolbar=no,dependent=no, width=865, height=600, resizable=yes, screenX=200, screenY=200, personalbar=no, scrollbars=yes";
    
        var newWin = window.open("", "_blank", winAttr);
        var writeDoc = newWin.document;
        writeDoc.open();
        writeDoc.write('<!doctype html><html> <style>.fc-header-toolbar{display :none !important;} .fc-scroller.fc-scroller-liquid{overflow:visible !important;} .fc-view-harness.fc-view-harness-active{height:auto !important;}</style>' + docHead + '<body onLoad="window.print()">' + printContents + '</body></html>');
        writeDoc.close();
        newWin.focus();
      }

使用它,您可以打印日历

相关内容

  • 没有找到相关文章

最新更新