1. 패키지 추가
const fs = require('fs'); // 파일 시스템 모듈을 가져옴
const path = require('path'); // 경로 모듈을 가져옴
const PDFDocument = require('pdfkit'); // pdfkit 모듈을 가져옴
2. PDF 부분 작성
const pdfDoc = new PDFDocument(); // PDF 문서 생성
res.setHeader('Content-Type', 'application/pdf');
res.setHeader(
'Content-Disposition',
'inline; filename="' + invoiceName + '"'
); // inline : 브라우저에서 열림, attachment : 다운로드
pdfDoc.pipe(fs.createWriteStream(invoicePath)); // 파일 스트림 생성
pdfDoc.pipe(res); // response 스트림 생성
// 한글 폰트 설정
const fontPath = path.join('fonts', 'NANUMGOTHIC.ttf');
pdfDoc.font(fontPath);
pdfDoc.fontSize(26).text('주문서', {
underline: true,
});
pdfDoc.text('-----------------------');
order.products.forEach(prod => {
pdfDoc.text(prod.product.title + ' - ' + prod.quantity + ' x ' + prod.product.price + '원');
});
pdfDoc.text('-----------------------');
pdfDoc.fontSize(20).text('총 금액: ' + order.products.reduce((acc, prod) => {
return acc + prod.quantity * prod.product.price;
}, 0) + '원');
pdfDoc.end(); // PDF 문서 종료