본문 바로가기
개발/javascript

현 페이지에서 특정 영역 인쇄, pdf 저장 기능

by 향유 2021. 8. 18.
function register(type){
/*
    if(form.company.value==''){
        alert('기업명을 입력해 주세요.'); form.company.focus(); return false;
    }
    if(form.name.value==''){
        alert('담당자명을 입력해 주세요.'); form.name.focus(); return false;
    }
    if(form.title.value==''){
        alert('견적서명을 입력해 주세요.'); form.title.focus(); return false;
    }
*/
    if(type=='click_print'){
        click_print();
    }else if(type='click_pdf'){
        click_pdf();
    }
}


function click_print(){
        /** 프린트 버튼 클릭 시 이벤트 */
            let container = $('#onlineQuoteModal').clone()    // 프린트 할 특정 영역 복사
            /*let cssText = ''                            // 스타일 복사
            for (const node of $('style')) {
                cssText += node.innerHTML
            }*/
            /** 팝업 */
            let innerHtml = container[0].innerHTML
            let popupWindow = window.open('', '_blank', 'width=700,height=800')
            popupWindow.document.write('<!DOCTYPE html>'+
              '<html>'+
                '<head>'+
                //'<style>'+cssText+'</style>'+
                '<meta name=\"viewport\" content=\"width=1240\">'+
                '<link rel=\"stylesheet\" type=\"text/css\" href=\"/html/_skin/mdp/css/fonts/fonts.css\">'+
                '<link rel=\"stylesheet\" type=\"text/css\" href=\"/html/_skin/mdp/css/main.css\">'+
                '<link rel=\"stylesheet\" type=\"text/css\" href=\"/html/_skin/mdp/css/sub.css\">'+
                '</head>'+
                '<body>'+
                '<div id=\"printWrap\">'+innerHtml+'</div>'+
                '</body>'+
              '</html>')

            popupWindow.document.close()
            popupWindow.focus()

            /** 1초 지연 */
            setTimeout(() => {
                popupWindow.print()         // 팝업의 프린트 도구 시작
                popupWindow.close()         // 프린트 도구 닫혔을 경우 팝업 닫기
            }, 1000)
}
function click_pdf(){

            let container = $('#onlineQuoteModal').clone()    // 프린트 할 특정 영역 복사
            /*let cssText = ''                            // 스타일 복사
            for (const node of $('style')) {
                cssText += node.innerHTML
            }*/
            /** 팝업 */
            let innerHtml = container[0].innerHTML
            let popupWindow = window.open('', '_blank', 'width=1250,height=1600')
            popupWindow.document.write('<!DOCTYPE html>'+
              '<html>'+
                '<head>'+
                //'<style>'+cssText+'</style>'+
                '<meta name=\"viewport\" content=\"width=1250\">'+
                '<link rel=\"stylesheet\" type=\"text/css\" href=\"/html/_skin/mdp/css/fonts/fonts.css\">'+
                '<link rel=\"stylesheet\" type=\"text/css\" href=\"/html/_skin/mdp/css/main.css\">'+
                '<link rel=\"stylesheet\" type=\"text/css\" href=\"/html/_skin/mdp/css/sub.css\">'+
                '</head>'+
                '<body>'+
                '<div id=\"printWrap\" style=\"background:#ffffff\">'+innerHtml+'</div>'+
                '</body>'+
              '</html>')

            popupWindow.document.close()
            popupWindow.focus()
            setTimeout(function() {
                html2canvas(popupWindow.printWrap, {
                    allowTaint: true, useCORS: true, backgroundColor: 'rgba(0,0,0,0)', removeContainer: true, x: 0, y: 0,
                    onrendered : function(canvas) {
                        // 한글깨짐현상때문에 jpeg->jspdf 전환
                        var imgData = canvas.toDataURL('image/jpeg',1.0);
                        var pageWidth = 1100; //가로 길이 a4 기준
                        var pageHeight = pageWidth * 1.414; //출력 페이지 세로길이
                        var imgWidth = pageWidth - 20;
                        var imgHeight = canvas.height * imgWidth / canvas.width;
                        var heightLeft = imgHeight;
                        var doc = new jsPDF('p','mm',[pageHeight, pageWidth]);
                        var position = 10;
                        doc.addImage(imgData, 'JPEG', 10, position, imgWidth, imgHeight);
                        heightLeft -= pageHeight;
                        // 한 페이지 이상일 경우 루프 돌면서 출력
                        while (heightLeft >= 10) {
                          position = heightLeft - imgHeight;
                          doc.addPage();
                          doc.addImage(imgData, 'JPEG', 10, position, imgWidth, imgHeight);
                          heightLeft -= pageHeight;
                        }
                        doc.save('화면.pdf');
                    }
                });
            }, 1000);

}

$this->addScriptFile("https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js");
$this->addScriptFile("https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js");

댓글