Skip to main content

CODE - JQuery - How to copy to clipboard and save as PDF


<code>
<pre id="text_to_copy">
this is a text
to 
copy with this format.
150games.com
</pre>
</code>

<button onclick="copyToClipboard('#text_to_copy')">Copiar al portapapeles</button>
<script>
function copyToClipboard(element) {
  if(element !== null){
    var $temp = $("<textarea>");
    var brRegex = /<br\s*[\/]?>/gi;
    $("body").append($temp);
    $temp.val($(element).text().replace(brRegex, "\r\n")).select();
    document.execCommand("copy");
    $temp.remove();
  }
}
</script>

<button id='save'>Save as PDF</button>
<script>
$("#save").click(function() {
  var text = $("#text_to_copy")[0].outerHTML;
  var styles = $("style")[0].outerHTML;
  var popup = window.open("", "popup");
  popup.document.body.innerHTML = text + styles;
  popup.focus();
  popup.print();
});
</script>