@spfxappdev/docxmerger
v1.0.2
Published
This is a merge/copy of the [docx-merger package](https://github.com/apurvaojas/docx-merger). But since the package has not been updated for more than 5 years and the package has some bugs regarding "corrupt" Word files, I created my own package. This pac
Downloads
35
Maintainers
Readme
@spfxappdev/docxmerger
This is a merge/copy of the docx-merger package. But since the package has not been updated for more than 5 years and the package has some bugs regarding "corrupt" Word files, I created my own package. This package also has TypeScript support and the API has been changed a bit. The JSZip npm package has also been updated. This package (unpacked) is about 1.5MB smaller than the original docx-merger package
Installation
npm install @spfxappdev/docxmerger
Usage
The example shows how an input field of type "File" is handled after the selection has been changed. The input field allows multiple selections:
HTML
<input type="file" id="wordFileInput" multiple accept=".docx" />
TypeScript
import { DocxMerger } from '@spfxappdev/docxmerger';
const fileInput = document.getElementById('wordFileInput') as HTMLInputElement;
fileInput.addEventListener('change', async (event) => {
const wordFiles = fileInput.files as FileList;
if(wordFiles.length < 2) {
alert("Please select at least 2 files");
return;
}
//All loaded files in an array of ArrayBuffer
const promises: Promise<ArrayBuffer>[] = [];
//Load all files as arrayBuffer and then resolve the "Promise"
const readFilesPromise = new Promise<void>((res, rej) => {
for (let fileIndex = 0; fileIndex < (wordFiles as FileList).length; fileIndex++) {
const reader = new FileReader();
const wordFile = wordFiles[fileIndex];
reader.onload = function (event) {
const arrayBuffer = event.target.result;
promises.push(Promise.resolve<ArrayBuffer>(arrayBuffer as ArrayBuffer));
if (promises.length === wordFiles.length) {
res();
}
};
reader.readAsArrayBuffer(wordFile);
}
});
await readFilesPromise;
const filesToMerge = await Promise.all(promises);
const docx = new DocxMerger();
await docx.merge(filesToMerge);
const mergedFile = await docx.save();
//DO something with the merged file
});
Demo
In this codepen you will find an example implementation including the code on how to download the merged file after merging
API
merge(files: JSZipFileInput[], options?: DocxMergerOptions)
This method merges the passed files into a single file.
Arguments
| name | type | description |
|-------|---------|-------------------------------------|
| files | JSZipFileInput
( ==>ArrayBuffer
or Uint8Array
or Blob
) | the files to merge |
| options | DocxMergerOptions
| Optional
options for the merging and saving process |
Type DocxMergerOptions
| name | type | description |
|-------|---------|-------------------------------------|
| pageBreak | boolean
| Determines whether a page break should be inserted after merging the file(s) |
| jsZipLoadOptions | JSZip.JSZipLoadOptions
| Optional
parameters that can be passed to JSZip and the loadAsync method when the merge method is called see JSZip |
| jsZipGenerateOptions | JSZip.JSZipGeneratorOptions
| Optional
parameters that can be passed to JSZip and the generateAsync method when the save method is called see JSZip Default value: { type: 'arraybuffer', compression: 'DEFLATE', compressionOptions: {level: 4} }
|
save()
Creates/saves the merged file async and returns it