Amberg / DocxTemplater

DocxTemplater is a library to generate docx documents from a docx template. The template can be bound to multiple datasources and be edited by non-programmers. It supports placeholder replacement and loops and images
MIT License
15 stars 1 forks source link

HTML Placeholder issue #23

Open rst30 opened 1 day ago

rst30 commented 1 day ago

Hello, I am currently trying to insert HTML snippet/code into a template. I set up a very simple template like so : image

Is this correct usage of the html placeholder?

I am using the code below to generate my file

using (MemoryStream generatedDocument = new MemoryStream())
{
    string htmlValue = "insert html text here";
    var templatePath = Path.Combine(_env.ContentRootPath, "InareTemplates", "TEST_TEMPLATE.docx");
    var template = DocxTemplate.Open(templatePath);
    template.BindModel("ds", new { CLAUSES = htmlValue });
    template.Save(generatedDocument);
    return File(generatedDocument.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "RenderedDocument.docx");
}

However, upon running it, it ran into an exception at this line template.Save(generatedDocument); With the below error : System.NullReferenceException: 'Object reference not set to an instance of an object.'

Am I using the html template/placeholder wrong? The example in the readme does not explain how to use it with the model.

Here is the html code sample I am using

<h1>The Main Languages of the Web</h1><p>HTML is the standard markup language for creating Web pages. HTML describes the structure of a Web page, and consists of a series of elements. HTML elements tell the browser how to display the content.</p><hr><p>CSS is a language that describes how HTML elements are to be displayed on screen, paper, or in other media. CSS saves a lot of work, because it can control the layout of multiple web pages all at once.</p><hr><p>JavaScript is the programming language of HTML and the Web. JavaScript can change HTML content and attribute values. JavaScript can change CSS. JavaScript can hide and show HTML elements, and more.</p>

Kind regards

rst30 commented 1 day ago

Also would like to add, returning the template into the very basic example in the readme made it work without errors, passing the raw html text inside Template : image Result : image

rst30 commented 1 day ago

I modified my code to somewhat follow your example in the tests folder,

using (MemoryStream generatedDocument = new MemoryStream())
{
    string htmlValue = htmlContent; 
    htmlValue = "html code here";

    var templatePath = Path.Combine(_env.ContentRootPath, "InareTemplates", "TEST_TEMPLATE.docx");
    var template = DocxTemplate.Open(templatePath);
    template.BindModel("ds", htmlValue);
    template.Process();
    template.Validate();
    template.Save(generatedDocument);
    return File(generatedDocument.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "RenderedDocument.docx");
}

This is the error that i could glance

at DocxTemplater.Formatter.VariableReplacer.ApplyFormatter(PatternMatch patternMatch, ValueWithMetadata valueWithMetadata, Text target)
   at DocxTemplater.Formatter.VariableReplacer.ReplaceVariables(OpenXmlElement cloned)
   at DocxTemplater.TemplateProcessor.ProcessNode(OpenXmlCompositeElement rootElement)
   at DocxTemplater.DocxTemplate.Process()