I created a datatable that includes options for PDF and print functionality. Unfortunately, the images within the datatable are not displaying in either the PDF or print view

I created a data table with PDF and print buttons, but I am facing an issue where the images in the table are not visible in the PDF or print view. Although I have attached logos to the data table that are visible in the PDF and print view, the existing images or Font Awesome icons are not showing up.

HTML

<table id="example" class="display responsive nowrap" style="width:100%" data-page-length='3'>
    <thead>
        <tr>
            <th>Data</th>
            <th>Data</th>
            <th>Data</th>
            <th>Data</th>
            <th>Data</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td><img src="./edit.png" class="edit-icon"></td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td><img src="./edit.png" class="edit-icon"></td>
        </tr>
        <tr>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td>Data</td>
            <td><img src="./edit.png" class="edit-icon"></td>
        </tr>
    </tbody>
</table>

JS

<script>
        $(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Blfrtip',
        buttons: [
            {
                extend: 'pdfHtml5',
                customize: function ( doc ) {
                    doc.content.splice( 1, 0, {
                        margin: [ 0, 0, 0, 12 ],
                        alignment: 'center',
                        image: 'IMAGE DATA'
                    } );
                }
            },
            {
                extend: 'print',
                customize: function ( win ) {
                    $(win.document.body).prepend('<img src="image.png"');
                }
            },
            
            'copy', 'csv', 'excel'
        ]
        
    } );
} );
    </script>

CSS

@media print {
    #example {
    margin-top: 100px;
    }
    .edit-icon {
         background: url(edit.png);
         width: 25px;
    height: 25px;
    padding: 5px;
    border-radius: 4px;
    display: block;
    }
}

CSS Links

<!-- Data Table & Data Table Buttons -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.3/css/jquery.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/2.3.5/css/buttons.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs5/jq-3.6.0/jszip-2.5.0/dt-1.13.2/af-2.5.2/b-2.3.4/b-html5-2.3.4/b-print-2.3.4/cr-1.6.1/date-1.3.0/fc-4.2.1/kt-2.8.1/r-2.4.0/rg-1.3.0/rr-1.3.2/sc-2.1.0/sb-1.4.0/sp-2.1.1/sl-1.6.0/sr-1.2.1/datatables.min.css" />
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.2/css/jquery.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
    <script src="https://cdn.datatables.net/v/bs5/jq-3.6.0/jszip-2.5.0/dt-1.13.2/af-2.5.2/b-2.3.4/b-html5-2.3.4...

JS Plugins

<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.13.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.5/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.5/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.5/js/buttons.print.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.4.0/js/dataTables.responsi...

How can I ensure that the icons/images are printed as well?

See HTML view of the DataTable https://i.sstatic.net/9su3x.jpg

Answer №1

When dealing with pdf plugins, it seems they struggle to read images directly from the site. However, if you provide a static URL like /usr/bla/bla, the pdf plugin can successfully read and download the image. There are instances where pdf plugins are able to read the image, but it is not consistent. The challenge arises when trying to input these URLs into your datatables.

An alternative solution is to have the pdf function outside of the datatable for exporting purposes. This can be achieved by utilizing PHP or other programming languages to export all data into a pdf format. It's important to consider the image URL issue - instead of using url(), implement a function that displays the real path to the image.

If you encounter problems with css loading properly during PDF exports, try using native css styles for a smoother transition.

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

Select an image and have it uploaded automatically

Is there a way to make it so that when I click on a button, the file browser opens up from my local disk and automatically uploads the selected file to a remote server? <input hidden type="file" id="loader" accept="image/*"> Javascript Code: $(&ap ...

Error with image loading event - inability to load custom image from user-provided URL or input - load() function malfunctioning

Is there a way to trigger a simple event (such as changing HTML in span) when an image from a custom or user-defined URL finishes loading? You can find an example of what I'm trying to achieve in this JSFiddle link. $('#btn').click(functio ...

Issue with Navbar Collapse Toggle not deactivating (following transition from bootstrap v4 beta to v4 alpha 6)

Recently, I encountered an issue with my navbar after switching to v4 alpha 6 in order to utilize the responsive utilities that this version offered. If you visit the following page: , you will see what I mean. Prior to upgrading to v4 alpha 6, all menu ...

How to dismiss a jQueryMobile dialog without triggering a page refresh

I've encountered a question similar to this before, but there wasn't any solution provided. The issue I'm facing is that I have a form and when a user clicks on a checkbox, I want to open a popup/dialog for them to enter some data. However, ...

Looking for solutions to troubleshoot my custom modal script - or seeking out any alternatives?

At the moment, our modal iframes are powered by blockUI and "yes/no"-dialogs are handled by jQuery UI dialog. The decision to use these components was based on a few factors: Both components seamlessly integrate with the jQuery UI theme, ensuring visual ...

Showing JSON Array Values in a Table

I have created an array and am attempting to display its values in a table. Initially, my solution only displayed a single value from the array that matched an exact ID. You can see this implementation at (). Entering "jjones" would yield a result. I then ...

The automatic message retrieval feature on the website for the PHP chat application is currently experiencing technical

When developing a PHP chat application, I have set status='0' for unread messages and status='1' for read messages as notifications. To enable auto-fetching of messages when a new message is received in the chat.php script, I have imple ...

Registration of Laravel Vue.js components

I am currently working on a Vue.js application in conjunction with Laravel. To get started, I registered Vue.js like this: import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); import App from './compone ...

What is the method for retrieving this data using Javascript?

I received this JSON-encoded data: { "error": { "msg":"Concurrent verifications to the same number are not allowed", "code":10 } } and I tried to access the 'msg' value using JavaScript as follows: $("#buttonPhoneSubmit ...

Troubleshooting issue with Gulp watch on Node v4.6.0

I'm currently facing a frustrating situation. I had a project up and running smoothly with a functioning gulpfile.js file, everything was perfect until I updated node to version 4.6.0. When I tried to report this issue on Gulp's git repository, t ...

Each container has its own div counter

Help needed to complete this code. The task is to count the number of .resp-containers in each container separately. Then, based on that count, assign a corresponding class to each element within the containers. Check out the code here $(document).ready(f ...

Laravel and Vue: Dealing with a Common CSRF Error in Two Similar Projects

I am currently working on two projects that have the same Laravel base code, but one project has a more advanced Vue.js part. Recently, I encountered a strange error message in the JavaScript console. CSRF token not found: https://laravel.com/docs/csrf#cs ...

I'm puzzled as to why my Vuex modules are not functioning properly. I keep receiving the error message: "[vuex]

I've been searching for hours and can't figure out why I keep getting this error message: [vuex] unknown mutation type: groceryStore/getStoreApple on all of the commits to the mutations in the groceryStore-module. I believe I'm following the ...

Utilizing Vuejs to dynamically set an id tag in a web application

I am working on a vue.js template with a todo prop, and I would like to dynamically set the id value of each element. Currently, my code snippet looks something like this. Is it possible to achieve what I want with this approach or are there other altern ...

Move files in Node.js without removing the original source directory

Currently, I am facing an issue while attempting to transfer files from one directory to another using the mv module. The problem arises when the files are successfully moved, but the source directory is automatically deleted. My intention is for only the ...

Creative Blueprint

Our company currently operates 3 browser based games with a team of just 4-5 coders. We are looking to enhance the collaboration within our coding team and streamline our processes. Considering the fact that our games are built using a mix of html, php, j ...

List of elements inside an HTML box

In my current HTML project, I am trying to include a box on a webpage that contains a scrollable list of elements. I want the content inside the box to scroll independently without affecting the rest of the page. Is there a way to achieve this? ...

Creating a div element only if a specific option input is chosen

I am working on a contact form for a client who wants to include a 'how did you find us' option. I plan to implement this using select and options, but the client also requested that if 'recommended' is selected, a box should appear ask ...

Hiding both clear buttons in the MUI Autocomplete component on Chromium: A simple guide

The issue with the clear button is specific to Chromium; it does not appear in Firefox. Take a look at an example of a MUI React Autocomplete component displaying two clear buttons. Various solutions mentioned in this thread only hide the rightmost butto ...

Obtain decrypted information from the token

I am facing difficulty in retrieving decrypted user data for the current user. Every time a user logs in, they receive a token. After logging in, I can take a photo and send it to the server. Looking at my code, you can see that this request requires a to ...