打印多张发票只需一次点击 TypeScript

huangapple go评论173阅读模式
英文:

Print multiple invoices in one click typeScript

问题

我正在尝试实现一键打印多个自定义HTML发票,就像这样:

打印多张发票只需一次点击 TypeScript

我尝试通过以下方式来实现,但这并不正确,因为它会多次显示打印对话框:

printInvoices() {
    this.orders.forEach(order => {
        let content = `
        <head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
            integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
        <style>
            @media print {
                .invoice {
                    font-size: 11px !important;
                    overflow: hidden !important
                }
            }
        </style>
        </head>

        <body>

        </body>
        `;

        let newWindow = window.open("", "", "width=300,height=300");
        newWindow?.document.write(content);
        newWindow?.focus();
        newWindow?.print();
        newWindow?.close();
    });
}

所以,请问有什么建议吗?

英文:

I'm trying to print multiple custom HTML invoices in one click, like this:

打印多张发票只需一次点击 TypeScript

I tried to implement it by this way, but it's not correct cause it will show the dialog print several times:

printInvoices() {
    this.orders.forEach(order =&gt; {
      let content = `
      &lt;head&gt;
    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;
        integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
    &lt;style&gt;
        @media print {
            .invoice {
                font-size: 11px !important;
                overflow: hidden !important
            }
        }
    &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
—

&lt;/body&gt;
      `;

      let newWindow = window.open(&quot;&quot;, &quot;&quot;, &quot;width=300,height=300&quot;);
      newWindow?.document.write(content);
      newWindow?.focus();
      newWindow?.print();
      newWindow?.close();
    });
  }

So, any advice please?

答案1

得分: 2

你可以通过在一个页面中以块的形式呈现所有发票,然后使用page-break-after: always;来强制将这些块分页打印。

示例:

<div class="container">
  <div class="pages">
    <h1>One</h1>
  </div>
  <div class="pages">
    <h1>Two</h1>
  </div>
  <div class="pages">
    <h1>Three</h1>
  </div>
</div>
<button id="printBtn">打印</button>

在多次使用window.print()会多次打开打印弹出窗口。

英文:

You can do that by rendering all your invoices in one page in blocks, then you use page-break-after: always; to force breaking these blocks to multiple pages

Example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.getElementById(&#39;printBtn&#39;).addEventListener(&#39;click&#39;, () =&gt; {
  window.print();
});

<!-- language: lang-css -->

.pages {
  display: none;
}

@media print {
  #printBtn {
    display: none;
  }
  @page {
    margin: 0;
  }
  .pages {
    page-break-after: always;
    display: block;
  }
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;pages&quot;&gt;
    &lt;h1&gt;One&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class=&quot;pages&quot;&gt;
    &lt;h1&gt;Two&lt;/h1&gt;

  &lt;/div&gt;
  &lt;div class=&quot;pages&quot;&gt;
    &lt;h1&gt;Three&lt;/h1&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;button id=&quot;printBtn&quot;&gt;Print me&lt;/button&gt;

<!-- end snippet -->


Having multiple window.print() means that it will open the print pop-up multiple times

答案2

得分: 1

可能的解决方案将是在服务器端合并多个PDF文件,然后一次性打印合并后的文件。

英文:

Probably, the solution will be to merge multiple PDFs on the server side and then print the merged file all at once.

huangapple
  • 本文由 发表于 2023年2月8日 16:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定