question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when trying to display Image

See original GitHub issue

I want to display a Logo at the start of the PDF-File but it’s not working. I get this Error:

Uncaught TypeError: Cannot read property ‘nodeType’ of null at jspdf.min.js:1245.

function tbl1ToPDF() {
            var base64Img = null;
            imgToBase64('[PATH]', function (base64) {
                base64Img = base64;
            });
            var pageContent = function (data) {
                doc.setFontSize(10);
                doc.addImage(base64Img, 'PNG', data.settings.margin.left, 15, 10, 10);
                var str = "[TEXT] " + data.pageCount;
                doc.setFontSize(10);
                doc.text(str, data.settings.margin.left, doc.internal.pageSize.height - 10);
            };
            var currentdate = new Date();
            var datetime = currentdate.getDate() + "/" + (currentdate.getMonth() + 1) + "/" + currentdate.getFullYear();
            var doc = new jsPDF();
            doc.setFontSize(8);
            doc.text("[TEXT] " + datetime, 135, 16);
            doc.setFontSize(20);
            doc.text("[TEXT]", 10, 26);
            var elem = document.getElementById("tbl1");
            var res = doc.autoTableHtmlToJson(elem);
            doc.autoTable(res.columns, res.data, {
                addPageContent: pageContent,
                startY: 30
            });
            doc.save("[NAME].pdf");
            return doc;
        }

This is the imgToBase64 function:

        function imgToBase64(url, callback) {
            if (!window.FileReader) {
                callback(null);
                return;
            }
            var xhr = new XMLHttpRequest();
            xhr.responseType = 'blob';
            xhr.onload = function () {
                var reader = new FileReader();
                reader.onloadend = function () {
                    callback(reader.result.replace('text/xml', 'image/jpeg'));
                };
                reader.readAsDataURL(xhr.response);
            };
            xhr.open('GET', url);
            xhr.send();
        }

Please help me fix this error. Huge Thanks for this awesome Plugin btw!

EDIT: I also tried to change the line

callback(reader.result.replace('text/xml', 'image/jpeg))' to callback(reader.result.replace('text/xml', 'image/png))' but it’s not working too…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
simonbengtssoncommented, Jul 24, 2017

👍You too!

0reactions
anandangurucommented, May 17, 2018

Your Example looks cool.!!But, How to deal with multiple URLs??

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when trying to display an image - Stack Overflow
I have a jQuery code that is supposed to display an image on a button click and to make it disappear on ajax-complete...
Read more >
Solved: Error when trying to display user photo and sample...
I have created a company directory using the office365 connector. The app works as expected - lists all active user within my domain...
Read more >
401 Unauthorized error when trying to display images on my ...
Hello I'm trying to make a gallery with the images that I have in cloudinary using React. I have checked out the Admin...
Read more >
Error 404 in trying to display an image (final project)
Alright I found an answer. The problem is that Flask needs you to place any images you want to display in a specific...
Read more >
Unable to display image error - PentaxForums.com
The reason is that Windows can write 'hidden' files to the card, which the camera cannot read. If you are sure all images...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found