PDF 파일 합치기

반응형

PDF 합치기.html
0.00MB

 

 

아래는 html 소스 코드입니다.

---------------------------------------------

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>PDF 합치기(Merge)</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf-lib/1.17.1/pdf-lib.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.10.377/pdf.min.js"></script>
    <style>
        #file-list { list-style: none; padding: 0; }
        #file-list li { padding: 8px; border: 1px solid #ccc; margin: 5px 0; display: flex; justify-content: space-between; align-items: center; }
        .move-buttons { display: flex; gap: 5px; }
        .move-buttons button { padding: 5px; }
    </style>
</head>
<body>
    <p><b>PDF 합치기</b>: 여려개의 PDF를 하나로 합쳐줍니다.</p>
    <p>---------------------------------------------------------------------------</p>
    <p>1. 아래 "파일 선택" 버튼을 누르고, 합칠 파일을 Ctrl 키를 누른 채 선택하세요.</p>
    <input type="file" id="file-input" multiple accept="application/pdf" />

    <p>2. 파일을 선택한 후 목록에서 ↑/↓ 화살표로 합칠 순서를 조정하세요.</p>
    <ul id="file-list"></ul>

    <p>3. 순서를 확인한 후 "PDF 합치기" 버튼을 누르세요.</p>
    <button id="merge-button">PDF 합치기</button>

    <script>
        const fileInput = document.getElementById('file-input');
        const fileList = document.getElementById('file-list');

        // 파일 선택 시 리스트에 추가
        fileInput.addEventListener('change', () => {
            fileList.innerHTML = '';
            Array.from(fileInput.files).forEach((file, index) => {
                const li = document.createElement('li');
                li.textContent = file.name;
                li.dataset.index = index;

                // 순서 변경 버튼 추가
                const moveButtons = document.createElement('div');
                moveButtons.classList.add('move-buttons');

                const upButton = document.createElement('button');
                upButton.textContent = '↑';
                upButton.addEventListener('click', () => moveItem(li, -1));

                const downButton = document.createElement('button');
                downButton.textContent = '↓';
                downButton.addEventListener('click', () => moveItem(li, 1));

                moveButtons.appendChild(upButton);
                moveButtons.appendChild(downButton);
                li.appendChild(moveButtons);

                fileList.appendChild(li);
            });
        });

        // 리스트 아이템 순서 변경 함수
        function moveItem(item, direction) {
            const sibling = (direction === -1) ? item.previousElementSibling : item.nextElementSibling;
            if (sibling) {
                fileList.insertBefore(item, direction === -1 ? sibling : sibling.nextElementSibling);
            }
        }

        // PDF 합치기
        document.getElementById('merge-button').addEventListener('click', async () => {
            const files = Array.from(fileList.children).map(li => fileInput.files[li.dataset.index]);
            const mergedPdf = await PDFLib.PDFDocument.create();

            for (let file of files) {
                const arrayBuffer = await file.arrayBuffer();
                const pdfDoc = await PDFLib.PDFDocument.load(arrayBuffer);
                const pages = await mergedPdf.copyPages(pdfDoc, pdfDoc.getPageIndices());
                pages.forEach(page => mergedPdf.addPage(page));
            }

            const pdfBytes = await mergedPdf.save();
            const blob = new Blob([pdfBytes], { type: 'application/pdf' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'merged.pdf';
            a.click();
            URL.revokeObjectURL(url);
        });
    </script>
</body>
</html>

반응형