1

I take a tutorial to print Google Charts from this document. Google Charts Documentary But I have some problems that I want to ask, maybe anyone can help me?

  1. How to save chart as image directly when i click link button?
  2. How to directly open the image to new tab when i click "print" button? I tried adding "target = blank" but it didn't work.

the real code

document.getElementById('png').outerHTML = '<a href="' + chart.getImageURI() + '">Printable version</a>';

and im trying to add target='_blank' here

    document.getElementById('png').outerHTML = '<a href="' + chart.getImageURI() + '" target="blank">Printable version</a>';

this my fiddle for testing the code

fiddle

AdityaDees
  • 1,022
  • 18
  • 39

2 Answers2

0

Sadly, you can't open a new tab with a data-uri URL, see this answer on Stack Overflow.

You will have to use window.open and then write content into it.

Pang
  • 9,564
  • 146
  • 81
  • 122
FGRibreau
  • 7,021
  • 2
  • 39
  • 48
-1

For your first issue, you can simply add a download attribute to your tag. See here: https://www.w3schools.com/howto/howto_html_download_link.asp

For the second issue, there was just a small problem with the quotation mark choice and placement. In this case, I just had to use double quotes around _blank instead, see below. A properly set up IDE can help with small syntax issues like this by highlighting different types.

  document.getElementById('png').outerHTML =
 '<a href="' + chart.getImageURI() + '" target="_blank">Printable version</a>';
atengel
  • 119
  • 5
  • thanks for your answer, but 1. is working, and 2 is not working – AdityaDees Oct 06 '18 at 22:13
  • Okay, I've just changed the solution. Please try it out now, this is working in my home environment. – atengel Oct 09 '18 at 15:12
  • there is no significant difference between your answers and my questions. both of them use target = 'blank'. and as I said before that target = "blank" does not work for me. – AdityaDees Oct 10 '18 at 21:09