Creating HTML content in a new window with Vue.js - a step by step guide

Recently, I encountered a problem with jsPDF regarding Unicode support in table generation. To work around this issue, I decided to utilize the browser's print feature instead. I achieved this by creating a new HTML document with the table and displaying it as a popup, then calling popup_window.print() as shown below:

//exporter.ts

  const temp = document.createElement("div");
  temp.innerHTML = tableHtml(
    tableHead,
    tableBody,
    fileName
  );
  const popup_window: any = window.open(location.origin, fileName, "x=y");
  popup_window.document.write(temp.innerHTML); 
  setTimeout(() => {
    popup_window.print();
    popup_window.close();
   }, 1000);

// tableHtml.ts

const tableHtml = (
  headers: string[],
  records: GeneralOBJ[],
  title: string,
  direction = "rtl"
) => `<!DOCTYPE html>
<html>
<body>
<main id="main" style="direction: ${direction};">
<h1>${title}</h1>
<div class="app_table">
<table class="table">
 <thead>
  <tr>
    ${headers
      .map(header => "<th class='col' dir='auto'>" + header + "</th>")
      .join("")}
  </tr>
</thead>
<tbody>
  ${records
    .map(
      record =>
        "<tr>" +
        Object.values(record)
          .map(td => "<td dir='auto'>" + td + "</td>")
          .join("") +
        "</tr>"
    )
    .join("")}
</tbody>
</table>
</div>

</main>

</body>
</html>`;

While this method works well, there are potential security risks such as xss vulnerabilities. To address this, I am considering using Vue.js to generate this piece of HTML content and then incorporate it into temp.innerHTML. Do you have any suggestions on how to achieve this securely?

Answer №1

VueJS serves as a powerful Single Page App tool, consolidating all elements into one cohesive HTML document. While it is feasible to operate multiple Vue instances across various HTML pages, synchronization of shared data or store is essential.

An effective strategy involves creating a component housing popup content, subsequently initiating an additional Vue instance on the popup window to mount the said component. For guidance on programmatically generating Vue component instances, refer to this insightful resource on CSS tricks.

In scenarios where simplicity is paramount, exploring alternative libraries can often offer quicker solutions. For tasks involving unicode compatibility, consult resources like this informative discussion on Stack Overflow for assistance with unicode support in PDF generation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the HTML code to display a table and a video/image side by side?

I'm facing a challenge while trying to create a website. I want to place a table in the center of the page, with videos on either side of it. However, whenever I attempt this, the table ends up below the videos instead of being aligned with them. I&ap ...

"Trouble with the accordion: How to make the first one open

Is there a way to make the first tab automatically expand when the page is refreshed? I want the General tab to be expanded by default like this: General (top header) (-) lorem ipsum (-) lorem ipsum doller amu site amu doller lorem ipsum (+) lorem i ...

Exploring Vue.js: Navigating Through an Array of Images Nested Within Another Array

I am looking to showcase images stored in an array called "image" within another array named product. Essentially, if a product has an array of 3 images, I want to display all 3 images, and so on. Here is the code snippet: <template> <div c ...

Creating a carousel with three thumbnails arranged in two rows

Currently, I am using a slider for my thumbnails. If you want to check out the code for this thumbnail slider, click on this link: thumbnails slider code My goal is to display 6 thumbnails in total, with 3 thumbnails shown in each row (2 rows total). Add ...

Utilizing variables in GraphQL requests

UPDATE: see the working code below GraphiQL Query I have this query for retrieving a gatsby-image: query getImages($fileName: String) { landscape: file(relativePath: {eq: $fileName}) { childImageSharp { fluid(maxWidth: 1000) { base64 ...

Transform a fabricjs canvas into a base64 encoded image

I am attempting to transmit a canvas as an image to my server in base64 format. While Fabricjs provides options such as canvas.toSVG() or canvas.toDataURL({format: 'image/png'}) to convert the canvas to an image, the output I see in my console ap ...

Learn to Generate a Mathematical Quiz with Javascript

For a school project, I am tasked with developing a Math Quiz which showcases questions one at a time. The questions vary in type, including Multiple Choice, Narrative Response, Image Selection, Fill in the blank, and more. I require assistance in creatin ...

Having trouble retrieving the array index of JSON data in Vue JS?

I have a project that includes data from a JSON API and I'm trying to display the index of each data. For example, I can view the floors from array index 0 to 22, but I'm having trouble getting the array index for the flats on each floor. Each fl ...

Conceal elements and offspring in D3.js through the utilization of JavaScript or jQuery

I am currently customizing a fantastic force directed layout created by @eyaler (http://bl.ocks.org/eyaler/1058611) that offers multiple options. My goal is to conceal a specific node with its children using jQuery or D3 along with JavaScript, not directly ...

Utilize the Selectize feature on every dropdown menu within my component

I needed to implement Selectize on every select tag within my component. The function is necessary because I have selects that can be added dynamically through a button. export default { data() {}, methods: { applySelectize() { this.$next ...

Using Vue to create a component that binds an array as its data source

Having trouble binding an array as a variable and accessing it in the child component, resulting in the variable being undefined. Is there a way to pass an array from a view to a component so that the component can use this array to create a child componen ...

Transferring JavaScript to a different page using EJS

Feeling a little stuck here and could use some assistance. It seems like a simple issue, but I'm tired and could really use a fresh pair of eyes to take a look. I'm trying to pass the 'username' variable, but since I'm new to this ...

Guide on moving elements to a different list with the help of the Angular DragDrop CDK Service

I encountered a unique situation while working on my project where I needed to implement a drag and drop feature for multiple lists using Angular CDK's DragDrop service. While I was able to successfully move items within a single list, transferring an ...

Enhancing the model using Sequelize JS

Currently, I am in the process of developing a basic API using Express and Sequelize JS. Recently, I encountered an issue while attempting to update a record with req.School, but no changes seemed to occur. Despite checking the code below thoroughly, I did ...

Adjust the transparency of a separate image within a different container by hovering over another image container

Is my goal too complex to achieve? I am attempting to create a hover effect where the opacity of artwork1 and button1 changes simultaneously when hovered over. However, I am having trouble properly labeling my elements and getting them to interact as inten ...

Navigating the scope set by a specific string

Is there a more efficient method than iterating through child objects to access the value at a specific location in the $scope, identified by the attribute "Calendar.Scheduling.field.Appointment.ApptDateTime_Date"? I was hoping for a solution similar to an ...

What is the reason behind re-rendering pages specifically for error messages in express and nodejs?

In the world of web development, it is commonly accepted practice to re-render a page to display error messages and redirect to show success messages. While implementing this principle in my code using express js, I have encountered a few challenges. For ...

The relationship between two distinct servers: Express.js and Node.js

Working with the Express.js framework and Node.js, I am trying to make a request to an MS SQL database on the server side. My goal is to retrieve data (in JSON or array format) from the server and send it to the client side. I'm unsure about how to ...

Using a curly brace in a React variable declaration

After completing a react tutorial, I started customizing the code to suit my requirements. One specific section of the code involved a component that received a parameter called label. render() { const { label } = this.props; ... } For instance, I re ...

Invoke a PHP function located in a separate file using a JavaScript function to fetch data from a database

How can I properly call a PHP function from a JavaScript function located in a different file? I am trying to pass a variable from an onClick function in JavaScript to a PHP function, which will then access a MySQL database to retrieve data. However, my c ...