Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodebuffer is not supported by this platform while using docxjs with javascript (not nodejs/react/vuejs). #1272

Closed
GauravAshara opened this issue Oct 31, 2021 · 3 comments · Fixed by #1384

Comments

@GauravAshara
Copy link

I am trying to integrate docxjs to my website /browser. To Read the local file I use the below code.

  xhr1.open('GET', 'template.dotx', true);
  if (xhr1.overrideMimeType) {
    xhr1.overrideMimeType('text/plain; charset=x-user-defined');
  }

  xhr1.onreadystatechange = function(e) {
    if (this.readyState == 4 && this.status == 200) {

      window.docData=this.response;
      }
  };
  xhr1.send();

Called below function

function generate() {

    const importDotx = new docx.ImportDotx();
     importDotx.extract(window.docData).then((templateDocument) => {
        const doc = new docx.Document({
            sections: [
                {
                    children: [
                        new docx.Paragraph({
                            children: [
                                new docx.TextRun("Hello World"),
                                new docx.TextRun({
                                    text: "Foo Bar",
                                    bold: true,
                                }),
                                new docx.TextRun({
                                    text: "\tGithub is the best",
                                    bold: true,
                                }),
                            ],
                        }),
                    ],
                },
            ],
        },                    
        {
            template: templateDocument,
        });
        
        
        
        docx.Packer.toBlob(doc).then((blob) => {
            console.log(blob);
            saveAs(blob, "example.docx");
            console.log("Document created successfully");
        });
        
    });
     

    }

I got below error Uncaught (in promise) Error: nodebuffer is not supported by this platform

@dolanmiu
Copy link
Owner

The template feature is not properly supported right now, its not too documented. Thinking of removing

If you are looking for a templating solution, consider docx-templater

https://docxtemplater.com/

@simonbrunel
Copy link
Contributor

@GauravAshara Probably too late for you but in case other users try to use templates - that contain images - in the browser, here is how I setup webpack in a vue-cli project to get rid of this error (based on this article). Though, you will probably also experience issues described in #489.

npm install -S buffer
// vue.config.js
const { ProvidePlugin } = require('webpack');

module.exports = {
  configureWebpack: {
    resolve: {
      fallback: {
        buffer: require.resolve('buffer/'),
      },
    },
    plugins: [
      new ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
      }),
    ],
  },
};

@dolanmiu
Copy link
Owner

docx now has this feature out of the box! As of v8.0.0:

https://github.com/dolanmiu/docx/blob/master/demo/85-template-document.ts

This library can now read and write docx files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants