Jump to content

convert HTML with JavaScript to node.js


Recommended Posts

I am new to this forum and am not even sure this is the correct place to put this, so would like feedback if I need to put this somewhere else.

I have an HTML file I created to make a computation portable to any browser back when I was programming in c about 2000.  I wrote the calculation in JavaScript and used HTML TR and TD tags to process input and output.

I am learning about node.js and suspect that I can put the JavaScript code in a javaScript file and keep the HTML TR and TD tags as they are.

Given this, please advise as to what I need to focus on in node.js to make this happen.  I expect to know the entire node.js system eventually, but am looking to have results sooner than later and perhaps write an article about the process for others who have a similar starting point.

Thanks in advance,

Roy

Link to post
Share on other sites
  • 2 weeks later...

Start by installing the package:

 

npm install html-docx-js --save

Next, include it in your code:

 

const htmlDocx= require('html-docx-js');

Now we just need some HTML to convert. Commonly, you might have rich text editors that allow you to capture rich content from users, and this content is stored in a database with HTML intact. It's just a matter of you pulling that content out of the database and passing it to the module's function.

For example, let's say you're pulling records from a database table, and you want to combine the content areas, and then push that out to the browser as a download.

Note: I'm using Express as the front-end JavaScript framework here.

 

app.get('/course/:name/:id'), async (req, res) => {  
    const { name, id } = req.params;
    const pages = await getCoursePages(id);
    let html = '';
    for(let i = 0; i < pages.length; i++) {
            html += pages[i].page;
    }
    const docx = htmlDocx.asBlob(html);
    res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    res.setHeader('Content-Disposition', `attachment; filename=${ sluggify(name) }.docx`);
    res.setHeader('Content-Length', docx.length);
    res.send(docx);
});

In the above code, you can see that we simply loop over the records from the getCoursePages() method, and concatenate the page property of the individual record. Once we have this concatenated string (which contains HTML), we pass it to the asBlob() method of the html-docx-js package. This creates the necessary Microsoft Word DOCX format, and once you have that, you can send it to the browser after setting the appropriate headers.

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...