자바스크립트/jQuery를 사용하여 div 콘텐츠 인쇄하기

워드프레스 정보를 제공하는 블로그 Avada 2019. 6. 25. 15:26 • 댓글:

웹페이지 내의 특정 div 내의 콘텐츠만을 인쇄하고 싶은 경우가 있습니다. 그런 경우 stackoverflow에 나와 있는 코드를 참고할 수 있습니다.

예시)

HTML

<div id='DivIdToPrint'>
    <p>This is a sample text for printing purpose.</p>
</div>
<p>Do not print.</p>
<input type='button' id='btn' value='Print' onclick='printDiv();'>

JavaScript

function printDiv()
{
  var divToPrint=document.getElementById('DivIdToPrint');
  var newWin=window.open('','Print-Window');
  newWin.document.open();
  newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
  newWin.document.close();
  setTimeout(function(){newWin.close();},10);
}
// 출처: https://stackoverflow.com/questions/33732739/print-a-div-content-using-jquery

다른 방법으로 https://github.com/jasonday/printThis에서 다운로드할 수 있는 jQuery 인쇄 플러그인을 활용해볼 수도 있습니다.

기능을 직접 구현하는 것에 어려움을 겪거나 시간을 절약하고 싶은 경우 워드프레스 플러그인, javascript/jQuery 플러그인, PHP 코드 등 다양한 종류의 코드를 판매하는 CodeCanyon에서 원하는 기능(예: 슬라이더)의 Javascript/jQuery 플러그인이나 PHP 스크립트가 있는지 검색해볼 수 있습니다.

참고: