<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raul Beltrame</title>
    <description>The latest articles on DEV Community by Raul Beltrame (@raulbeltrame).</description>
    <link>https://dev.to/raulbeltrame</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1475533%2F5768acb4-ced9-4a43-9ee1-f950ccae8e0a.png</url>
      <title>DEV Community: Raul Beltrame</title>
      <link>https://dev.to/raulbeltrame</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raulbeltrame"/>
    <language>en</language>
    <item>
      <title>Printing reactjs component in a thermal printer</title>
      <dc:creator>Raul Beltrame</dc:creator>
      <pubDate>Tue, 07 May 2024 12:39:17 +0000</pubDate>
      <link>https://dev.to/raulbeltrame/printing-reactjs-component-in-a-thermal-printer-5g5j</link>
      <guid>https://dev.to/raulbeltrame/printing-reactjs-component-in-a-thermal-printer-5g5j</guid>
      <description>&lt;p&gt;I'm developing the management part of a delivery system and i am trying to generate a and print in a thermal printer the customers asks. Most of the users will be restaurants, so i wish i could print it without those dialog boxes just when i click in a button to 'print' to avoid an exausting work when they have a lot of asks.&lt;/p&gt;

&lt;p&gt;I can generate the pdf correctly, but it is not printing right. Thats what i've tryied:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Relatorio(props){

useEffect(() =&amp;gt; {
    const element = document.getElementById('wrap-relatorio-pedido'); 
    const opt = {
        margin: 5,
        filename: `pedido #${props.nbl_num_nota}.pdf`,
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: 'mm', format: 'a6', orientation: 'portrait' }
    };

    //first attempt
    // html2pdf().from(element).set(opt).outputPdf('arraybuffer').then((pdf) =&amp;gt; {
    //     const blob = new Blob([pdf], { type: 'application/pdf' });
    //     const blobUrl = URL.createObjectURL(blob);

    //     const iframe = document.createElement('iframe');
    //     iframe.style.display = 'none';
    //     iframe.src = blobUrl;
    //     document.body.appendChild(iframe);

    //     iframe.onload = () =&amp;gt; {
    //         iframe.contentWindow.print();
    //         document.body.removeChild(iframe);
    //         URL.revokeObjectURL(blobUrl);
    //     };
    // });


    //second attempt
    // var req = new XMLHttpRequest();
    // req.onload = function (event) {
    //     var blob = new Blob([req.response], {type: 'application/pdf'}); //this make the magic
    //     var blobURL = URL.createObjectURL(blob);

    //     const iframe =  document.createElement('iframe'); //load content in an iframe to print later
    //     document.body.appendChild(iframe);

    //     iframe.style.display = 'none';
    //     iframe.src = blobURL;
    //     iframe.onload = function() {
    //       setTimeout(function() {
    //         iframe.focus();
    //         iframe.contentWindow.print();
    //       }, 1);
    //     };
    // };

    html2pdf().from(element).set(opt).save();

}, [props.nbl_num_nota]);

const space = ' '; // adiciona espaçamento em spans

const data = new Date(props.nbl_dat_emissao);

// Formatar a data no formato dd/mm/yyyy
const dataFormatada = `${('0' + data.getDate()).slice(-2)}/${('0' + (data.getMonth() + 1)).slice(-2)}/${data.getFullYear()}`;

// Formatar a hora no formato hh:mm
const horaFormatada = `${('0' + data.getHours()).slice(-2)}:${('0' + data.getMinutes()).slice(-2)}`;

return &amp;lt;&amp;gt;
    &amp;lt;div className='d-flex justify-content-between pedido ps-2 pt-3 pb-1' id='body-relatorio-pedido'&amp;gt;
        &amp;lt;div className='row pt-4' id='wrap-relatorio-pedido'&amp;gt;
            //content
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I don't know if it is possible due to the browser security and i have alredy tryied to the other similar question's solutions... so if anyone know how to do it or can answer if it is possible or not would help a lot.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
