React 에서 PDF 파일 프린터 기능 구현하기
Post

React 에서 PDF 파일 프린터 기능 구현하기

React 에서 PDF url 또는 blob 을 프린터하는 기능을 구현해 본다.

예제

iframe 을 활용하여 프린터 기능을 간단하게 사용할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React from 'react';
import { Button } from 'antd';

const ExamplePrintPDF = () => {

  const printPdfByUrl = (pdfUrl) => {
    var iframe = document.createElement('iframe');
    iframe.style.display = "none";
    iframe.src = pdfUrl;
    document.body.appendChild(iframe);
    iframe.contentWindow.focus();
    iframe.contentWindow.print();
  }

  const printPdfByBlob = (pdfBlob) => {
    var iframe = document.createElement('iframe');
    iframe.style.display = "none";
    iframe.src = URL.createObjectURL(pdfBlob);;
    document.body.appendChild(iframe);
    iframe.contentWindow.focus();
    iframe.contentWindow.print();
  }

  return (
    <Button onClick={()=> {
        printPdfByUrl('https://.../test.pdf')
    }}>Print PDF</Button>
  )
}