open-xml-templating/docxtemplater

Did anybody used docxtemplate open source-version in Salesforce light web component?

Nithac opened this issue · 4 comments

Environment

  • Version of docxtemplater :
  • Used docxtemplater-modules :
  • Runner : Browser/Node.JS/...

How to reproduce my problem :

My template is the following : (Upload the docx file here inside github, which you have to name template.zip (github doesn't accept the docx extension))

With the following js file :

const fs = require('fs');
const Docxtemplater = require('docxtemplater');

const content = fs
    .readFileSync(__dirname + "/template.zip", "binary");

const zip = new PizZip(content);
const doc = new Docxtemplater(zip)

doc.render({
	( INSERT YOUR DATA HERE )
});

const buf = doc.getZip()
             .generate({type:"nodebuffer"});

fs.writeFileSync(__dirname+"/output.docx",buf);

I would expect it to :

  • return the following template (please upload your generated document and expected document)
  • not fail (include error message)

Yes, I think some of my users have done this, tough I don't think a lot of them are following the github issues here.

Do you have any specific difficulty during the integration ?

I will close this issue as there's nothing specific I can do at the moment.

Thank for getting back. I am trying to import docxtemplate into Salesforce lightning web component, but not successful yet.
Here is what I am trying, I have to open a new word doc(or use an existing template), populate data and then allow use to download a document.

Here is the sample code that I am using. I am not able to open/use an existing template. I am sure I am not doing it correctly. Do you have any sample code that I try?

import { LightningElement, api,wire,track } from 'lwc';
import {loadScript} from "lightning/platformResourceLoader";
import docxtemplateImport from "@salesforce/resourceUrl/docxtemplate";
import altDoc from '@salesforce/resourceUrl/GenerateDocAlternate'

export default class TestDocument extends LightningElement {

@api recordId;
@track records;   

connectedCallback(){
    console.log('altDoc -', altDoc);
    Promise.all([loadScript(this, docxtemplateImport)]).then(() =>{
        this.renderButtons();
    });
}

renderButtons(){
    this.template.querySelector(".hidden").classList.add("not_hidden");
    this.template.querySelector(".hidden").classList.remove("hidden");
}

startDocumentGeneration(){  
        this.buildDocument(); 
}

buildDocument(){
    console.log('before document');        
    let document = new docx.Docxgen(altDoc);
    console.log('after document');
    this.generateDownloadLink(document);
}

generateDownloadLink(documentPassed){
    docx.Packer.toBase64String(documentPassed).then(textBlob =>{
        this.downloadURL = 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,' + textBlob;
        this.template.querySelector(".slds-hide").classList.remove("slds-hide");
    });
}

}

It's working great in my lwc component:

  1. create docxtemplater folder in 'staticresources' folder
  2. add docxtemplater.js & jszip.js to that folder
    add the following code snippets:
import docxtemplater from "@salesforce/resourceUrl/docxtemplater";

  async connectedCallback() {
    try {
      Promise.all([
        loadScript(this, docxtemplater + "/docxtemplater.js"),
        loadScript(this, docxtemplater + "/jszip.js"),
      ]).catch((error) => {
        this.error = "Error loading libraries.";
        console.error("error loading libs", error);
      });
    } catch (error) {
      console.error("error", error);
      this.error = "Error.";
    }
  }

after that, you can invoke in the following way:

let zipContent = new JSZip(filecontent);
let doc = new Docxtemplater(zipContent);
doc.setData(replacementTags).render();
const generatedBase64 = doc.getZip().generate({
      type: "base64",
      mimeType:
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });

Thank you gennady9