Tips on revealing concealed information when creating a printable format of an HTML document

I need to find a way to transform an HTML table into a PDF using JavaScript or jQuery. The challenge I'm facing is that the table contains hidden rows in the HTML, and I want these hidden rows to also appear in the generated PDF.

Currently, when I convert the table to a PDF, the hidden rows are not included. The table displays some rows in the HTML, and then the user navigates to the next page of the table. I want all pages of the table to be visible in the PDF document.

Below is the code snippet in HTML and JavaScript:


    <!DOCTYPE html>
    <html>
    <body>
        <div id="tab">
            <table> 
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Job</th>
                </tr>
                <tr>
                    <td>Brian</td>
                    <td>41</td>
                    <td>Blogger</td>
                </tr>
                <tr>
                    <td>Matt</td>
                    <td>25</td>
                    <td>Programmer</td>
                </tr>
                <tr>
                    <td>Arun</td>
                    <td>39</td>
                    <td>Writer</td>
                </tr>

                <tr style="display: none">
                    <td>Arun</td>
                    <td>39</td>
                    <td>Writer</td>
                </tr>

                <tr style="display: none">
                    <td>Arun</td>
                    <td>39</td>
                    <td>Writer</td>
                </tr>

                <tr style="display: none">
                    <td>Arun</td>
                    <td>39</td>
                    <td>Writer</td>
                </tr>
            </table>
        </div>

        <p>
            <input type="button" value="Create PDF" 
                id="btPrint" onclick="createPDF()" />
        </p>
    </body>
    <script>
        function createPDF() {
            var sTable = document.getElementById('tab').innerHTML;

            var style = "<style>";
            style += "table {width: 100%;font: 17px Calibri;}";
            style += "table, th, td {border: solid 1px #DDD; border-collapse: collapse;";
            style += "padding: 2px 3px;text-align: center;}";
            style += "</style>";

            // CREATE A WINDOW OBJECT.
            var win = window.open('', '', 'height=700,width=700');

            win.document.write('<html><head>');
            win.document.write('<title>Profile</title>');   // <title> FOR PDF HEADER.
            win.document.write(style);          // ADD STYLE INSIDE THE HEAD TAG.
            win.document.write('</head>');
            win.document.write('<body>');
            win.document.write(sTable);         // THE TABLE CONTENTS INSIDE THE BODY TAG.
            win.document.write('</body></html>');

            win.document.close();   // CLOSE THE CURRENT WINDOW.

            win.print();    // PRINT THE CONTENTS.
        }
    </script>
    </html>

Answer №1

Instead of creating a PDF file in your code, it seems like you are generating an HTML page in a popup window for printing. To hide certain rows, you can utilize CSS to show or hide the required elements.

To accomplish this:

  1. In your main HTML page, create a class named "printonly" to be used for hiding rows: .printonly { display:none;} and include this in the <head>

  2. Assign the class to the rows you wish to hide, for example: <tr class="printonly">

That's all there is to it!

Here's how it works:

  • The .printonly style in your main HTML page will hide any rows with that class.
  • In the popup window for printing, only the contents of <div id="tab"> are included, which means the style for hiding the .printonly class is not present, so those rows remain visible.

I have outlined below the parts of your code that require adjustment (without the comments):

<!DOCTYPE html>
    <html>
    <head>
    <style> .printonly { display:none;} </style>   //<--  STEP 1. ADD THE STYLE
    </head>
    <body>
        <div id="tab">
         <table> 
             <tr>
                <th>Name</th>
                 <th>Age</th>
                 <th>Job</th>
              </tr>

             <tr class="printonly">   //<-- STEP 2. ADD THE CLASS TO THE "HIDDEN" ROWS
                  <td>Arun</td>
                  <td>39</td>
                  <td>Writter</td>
               </tr>
               // etc with the rest of your "hidden" table rows

            </table>
        </div>

       // Your page content and script remains unchanged.
    <html>

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

Guide on enabling a new input field in React when a dropdown option is selected by a user

I'm attempting to show an additional input field when the user selects "Other" from the dropdown menu. I have implemented a state for the input field that toggles between true and false based on the selected value from the dropdown. However, I am enco ...

What is the most efficient way to handle dependencies and instantiate objects just once in JavaScript?

I am interested in discovering reliable and tested design patterns in Javascript that ensure the loading of dependencies only once, as well as instantiating an object only once within the DOM. Specifically, I have the following scenario: // Block A in th ...

Clicking on Bootstrap Select Does Not Trigger Dropdown Opening

Currently, I am working on generating a dynamic set of bootstrap-select widgets that include a dropdown. My main challenge lies in preventing the select from closing the dropdown, which requires me to use 'event.stopPropagation()' on the dropdown ...

The CSS3 Animation remains stationary

I've been attempting to animate my block element moving to the left using CSS animation shorthand property, but unfortunately, it's not working as expected. Here is my HTML: <div id="game"> <div id="block">&l ...

Techniques for Grouping an Array of Objects by a Specific Value Key in Angular

I have the following array that needs to be grouped by section_name in HTML. Additionally, I need to first check if the length of the array values for section_name is greater than zero before displaying the results grouped by section_name. I hope my expl ...

What strategies can be employed to mitigate the activation of the losing arm in a Promise.race?

My current task involves sending the same query to multiple identical endpoints (about five) across various Kubernetes clusters. The goal is to aggregate the results without any delays and report failures to the user while continuing with the process seaml ...

Creating a personalized <select> filter in Angular.js with two different JSON choices

I am working with a Q&A JSON feed that includes the following: "questions": [ { "answer": "Ea et non sunt dolore nulla commodo esse laborum ipsum minim non.", "id": 0, "poster": "Chelsea Vang", "question": "Ex ex elit cu ...

Are MobX Observables interconnected with RxJS ones in any way?

Is the usage of RxJs observables in Angular comparable to that in React and MobX? I'm struggling to find information on this topic. ...

The collapse of HTML elements converging inwards within the Bulma Framework and customized with SASS

Messy Screen Recently delving into the world of Bulma, I decided to take a shot at customizing my CSS. Unfortunately, it seems that after some tweaking, my screen layout has gone haywire. Can anyone shed light on why two elements might refuse to maintain ...

What could be causing this Jasmine test to fail, and is it necessary for all functions to be within the scope in order to be tested

My inquiry involves two separate questions: Question One When conducting Jasmine based Angular testing, is it necessary for all functions to be on the scope in order to be tested? For instance, when I attempt to call a function within the test suite like ...

Navigating into iframe can be tricky when the previous iframe is not properly closed in Selenium Webdriver. Here

One issue we are facing is the excessive use of iframes in HTML. It has become troublesome to locate elements as some are buried within multiple layers of iframes. I'm trying to access an element inside the outermost iframe (the second one), but I&ap ...

Changes are not being detected in new Angular 2 files

Currently, I am enhancing an Angular 2 project by incorporating new modules. However, the changes I made in these files are not being recognized within the project. I have attempted to research how change detection functions in Angular 2, but have not bee ...

What are some possible reasons or solutions for Axios sending a 404 error?

Hi there, I'm completely new to the MERN stack and I'm encountering an issue when trying to post data using Axios and Express. I may have misunderstood something, so here's my problem. I have a form on a page that I am attempting to use to s ...

Using a forEach loop within the RequestAnimationFrame function

I am currently developing a project using THREEjs and would like to organize it in the following manner: class Blah scene : new THREE.Scene() renderer : new THREE.WebGLRenderer() camera : new THREE.PerspectiveCamera() ...

Ensuring the accuracy of URLs using JavaScript

I am developing a form with an input field for entering a URL and a submit button. I want to incorporate JavaScript validation to show an alert box when a correct URL is entered. Is it achievable using JavaScript? If so, can you please guide me on how to i ...

The function .load() is not functioning properly on iPads

I am experiencing issues with a simple script that is supposed to check an image on my iPad running iOS 5.1. The script is meant to load a stream of jpg images and work on each frame, similar to how it works in a large Safari browser. However, on the iPa ...

I encountered a permission denied error when trying to enter DEBUG=app ./bin/www in Node.js

After renaming 'my-application' to just 'app', I encountered an issue when running the DEBUG command in the terminal: I used DEBUG=app ./bin/www Originally, it was named 'my-application' as created by express. However, after ...

Directive that can be reused with a dynamic item name in ng-repeat

I've developed a reusable directive similar to a dropdown, but this dropdown opens in a modal and is functioning well. The structure of my directive is as follows: <p-select items="deptStations" header-text="Select " text="Select departure..." te ...

Incorporate jQuery UI Datepicker with the ability to trigger an Ajax function upon selecting a

Is it possible to execute Ajax on date select from a datepicker and how can I handle it? I am not receiving any errors in the console regarding the Ajax part. Instead of getting the pro-rated subscription value, I am seeing "success" in the div with ID &ap ...

Choose content within an HTML tag and modify its appearance

<body> This is text1 <div> This is text2</div> </body> I'm looking to style the text1 only. I attempted to use body{ color:red; } but both text1 and text2 ended up turning red. What I really need is something like th ...