Closed Nithac closed 1 year ago
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:
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
Environment
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 :
I would expect it to :