Created
December 18, 2022 11:48
-
-
Save leonidkuznetsov18/ecadcf888770192cf1dd0950eefcb208 to your computer and use it in GitHub Desktop.
PDF to Word converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the PDF as a Blob | |
const file = await fetch('path/to/pdf.pdf').then(res => res.blob()); | |
// Convert the PDF to a base64-encoded data URI | |
const reader = new FileReader(); | |
reader.readAsDataURL(file); | |
reader.onload = () => { | |
const base64 = reader.result; | |
// Create a new Blob from the data URI | |
const dataURI = base64.split(',')[1]; | |
const byteString = atob(dataURI); | |
const arrayBuffer = new ArrayBuffer(byteString.length); | |
const int8Array = new Uint8Array(arrayBuffer); | |
for (let i = 0; i < byteString.length; i++) { | |
int8Array[i] = byteString.charCodeAt(i); | |
} | |
const pdf = new Blob([arrayBuffer], { type: 'application/pdf' }); | |
// Use docx.js to convert the PDF to a Word file | |
const docx = new docx.Document(); | |
docx.addSection({ | |
// Add each page of the PDF to the docx.js document | |
PDFJS.getDocument(pdf).then(pdfDoc => { | |
for (let i = 1; i <= pdfDoc.numPages; i++) { | |
pdfDoc.getPage(i).then(page => { | |
const viewport = page.getViewport({ scale: 1 }); | |
const canvas = document.createElement('canvas'); | |
const context = canvas.getContext('2d'); | |
canvas.height = viewport.height; | |
canvas.width = viewport.width; | |
page.render({ canvasContext: context, viewport: viewport }).then(() => { | |
const image = canvas.toDataURL('image/png'); | |
docx.addParagraph(new docx.Paragraph(new docx.Picture(image))); | |
if (i < pdfDoc.numPages) { | |
docx.addParagraph(new docx.Paragraph()); | |
} | |
}); | |
}); | |
} | |
}); | |
}); | |
// Save the Word file | |
docx.download('pdf-to-word.docx'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment