- Saved searches
- Use saved searches to filter your results more quickly
- License
- Hargne/html-creator
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- How to generate an HTML table and a PDF with Node & Google Puppeteer
- So to again give a brief overview, things we will cover:
- Step 8:
- Step 9:
- HTML Templating in NodeJs
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Generate HTML with node.js
License
Hargne/html-creator
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
const htmlCreator = require("html-creator"); const html = new htmlCreator([ type: "head", content: [ type: "title", content: "Generated HTML", >, type: "style", content: ` #cool-text color: red; > `, >, ], >, type: "body", content: [ type: "div", content: [ type: "div", content: "This is a cool text 😎", attributes: id: "cool-text" >, >, type: "a", content: "Click here", attributes: href: "/path-to-infinity", target: "_blank" >, >, ], >, ], >, ]); const result = html.renderHTML();
The above code will result with the following HTML output:
> html> head> title>Generated HTMLtitle> style> #cool-text < color: red; > style> head> body> div> div id pl-s">cool-text">This is a cool text 😎div> a href pl-s">/path-to-infinity" target pl-s">_blank">Click herea> div> body> html>
Visit the wiki for more examples of usage, method reference and further reading.
Do you believe that something is missing from this plugin or perhaps is not working as intended? Awesome-pants! Help is always appreciated. Just be sure to read through the Contributing Handbook (and remember to have a jolly good time).
About
Generate HTML with node.js
How to generate an HTML table and a PDF with Node & Google Puppeteer
Adeel Imran
Understanding NodeJS internally can be a little bit daunting (I know it was for me once). Node is a very powerful language and it can do a lot of things.
Today I wanted to uncover the power of Node’s built-in utility tool called fs (file system)
The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.
Which is just a fancy way of saying that file system is a way in Node to interact with files for both read and write operations.
Now file system is a humongous utility in NodeJS that has a lot of fancy features. In this article, however I will only discuss 3:
- Getting file information: fs.statSync
- Deleting a file: fs.unlinkSync
- Writing data to a file: fs.writeFileSync
Another thing we will cover in this article is Google Puppeteer which is this really cool, slick tool created by some awesome folks at Google.
So what is puppeteer? Well as per the docs, they say:
Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.
So it’s basically a tool that lets you do all the cool browser related things on server. Like getting a website’s screenshots, crawling websites, and generating pre-render content for single page applications. You can even do form submissions via your NodeJS server.
Again puppeteer is a huge tool, so we will cover just a small but a very cool feature of puppeteer. We’ll look at how to generate a nice PDF file based on our generated HTML table file. In the process we’ll learn about puppeteer.launch() and understand a bit about page() & pdf().
So to again give a brief overview, things we will cover:
- Generating stub data (for invoices) using an online tool.
- Creating an HTML table with a little bit of styling with generated data in it, using an automated node script.
- Learning about checking if a file exists or not using fs.statSync
- Learning about deleting a file by using fs.unlinkSync
- Learning about writing a file using fs.writeFileSync
- Creating a PDF file of that HTML file generated using Google puppeteer
- Making them into npm scripts, to be used later ? ?
Also before we begin here is the entire source code of the tutorial, for everyone to follow along. You don’t have to write anything, but you should write code along with this tutorial. That will prove more useful & you’ll understand more. SOURCE CODE OF TUTORIAL
Before we begin, please ensure that you have at least the following installed on your machine
You don’t need to, but you can also watch an introductory video (my first ever made) that talks about the basics in reading, writing, and deleting a file in NodeJS. This will help you understand this tutorial. (Please do give me feedback). ?
Also you can add an npm script in your package.json like this:
This way instead of writing npm run ./createTable.js , you can just type in npm run build:table .
Next up: generating a PDF from the generated HTML file.
Step 8:
First things first we need to install a fancy tool, so go in your terminal in your application folder and type in
Step 9:
In the same folder where you have files createTable.js , buildPaths.js & data.json , create a new file called createPdf.js and add content to it like below:
And that is it, we are done.
You have learned the following:
- How to check if a file exists / tet file information (in Node)
- How to delete a file in Node
- How to write to a file
- How to use Google Puppeteer to generate a PDF file
Happy learning, I would love to hear your thoughts on this article. You can reach me on twitter as well.
HTML Templating in NodeJs
HTML templating is a technique that allows us to create a base HTML structure and use placeholders to dynamically generate content based on data retrieved from our JSON file or database. Let’s consider a hypothetical instance where our website consists of numerous product cards, each containing specific product details that are retrieved from the JSON file. Now, if we were to add or remove any products from our JSON file, how would we update the corresponding cards on the front-end dynamically? Considering our content-based data is stored in a JSON file, we can proceed with creating reusable templates from our existing HTML code. Step 1: Building the templates
As a developer, you’re probably familiar with the concept of serving dynamic web content. One way to achieve this is by using templates. We would create two HTML templates, one for the product overview page and one for the individual product cards. The first template template-card.html is used as a blueprint for the individual product cards, and the second template-overview.html is used as a blueprint for the overview page. These templates contain placeholders that will be replaced with actual content when the page is requested by a user. Ensure that your placeholder does not contain any symbols that are part of the HTML code. A commonly used syntax for placeholders is . Here is template-card.html our first template used as a blueprint to create as many cards as needed dynamically.