ray-template/docs/assets/reference_hooks_output.md.B8QXZTOo.js

23 lines
11 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{_ as i,o as a,c as e,a3 as t}from"./chunks/framework.BNdm2jZV.js";const c=JSON.parse('{"title":"DOM 截图与打印","description":"","frontmatter":{},"headers":[],"relativePath":"reference/hooks/output.md","filePath":"reference/hooks/output.md"}'),n={name:"reference/hooks/output.md"};function p(l,s,h,k,r,d){return a(),e("div",null,[...s[0]||(s[0]=[t(`<h1 id="dom-截图与打印" tabindex="-1">DOM 截图与打印 <a class="header-anchor" href="#dom-截图与打印" aria-label="Permalink to &quot;DOM 截图与打印&quot;">​</a></h1><h2 id="usedomtoimage" tabindex="-1">useDomToImage <a class="header-anchor" href="#usedomtoimage" aria-label="Permalink to &quot;useDomToImage&quot;">​</a></h2><p><code>useDomToImage(target, options?)</code> 返回 <code>create(format?)</code>。调用时从元素、ref 或 getter 解析目标,缺省格式为 <code>jpeg</code>,<code>options.imageType</code> 设置默认值,单次 <code>create(format)</code> 优先。Promise 按格式推断返回值:</p><table tabindex="0"><thead><tr><th>格式</th><th>返回</th></tr></thead><tbody><tr><td><code>jpeg</code>、<code>png</code>、<code>svg</code></td><td>数据 URL 字符串。</td></tr><tr><td><code>blob</code></td><td><code>Blob | null</code>。</td></tr><tr><td><code>canvas</code></td><td><code>HTMLCanvasElement</code>。</td></tr><tr><td><code>pixelData</code></td><td><code>Uint8ClampedArray</code>。</td></tr></tbody></table><p><code>options</code> 也可传 html-to-image 的选项,如 <code>backgroundColor</code>、<code>pixelRatio</code>、<code>filter</code>。<code>beforeCreate(element)</code> 在目标检查前调用,<code>created(result, element)</code> 在成功后调用;回调的结果类型涵盖全部格式,应先检查类型。<code>createdError(error)</code> 接收错误,Promise 仍以 <code>Error</code> 拒绝;<code>finally(element)</code> 每次都会调用,目标未解析时可能为空。截图要在目标挂载后执行,跨域图片和字体需满足浏览器加载限制。</p><div class="language-ts vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">ts</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { useDomToImage } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;@/hooks&#39;</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { ref } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;vue&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> function</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> useReportImage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> report</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ref</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">&lt;</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">HTMLElement</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">&gt;()</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">create</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> useDomToImage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(report, { imageType: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;png&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, pixelRatio: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> })</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> report,</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> exportPng</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: () </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=&gt;</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> create</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(),</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> exportCanvas</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: () </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=&gt;</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> create</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;canvas&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">),</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h2 id="useprint" tabindex="-1">usePrint <a class="header-anchor" href="#useprint" aria-label="Permalink to &quot;usePrint&quot;">​</a></h2><p><code>usePrint(target, options?)</code> 返回 <code>print()</code>,调用时解包目标。接受 DOM 目标、URL 或字符串、<code>Blob</code>、<code>Uint8ClampedArray</code>、对象数组。<code>UsePrintOptions</code> 是 print-js 配置中除 <code>printable</code> 外的字段。</p><ul><li>DOM 默认以 <code>html</code> 打印。字符串默认按 <code>pdf</code> 处理;图片 URL、JSON 数组请分别指定 <code>type: &#39;image&#39;</code>、<code>type: &#39;json&#39;</code>(JSON 同时传 <code>properties</code>)。</li><li>图片 MIME 的 Blob 默认以 <code>image</code> 处理,其他 Blob 和字节数组默认以 <code>pdf</code> 处理。二进制目标会先异步转为数据 URL,PDF 会去掉数据 URL 前缀。</li><li>空目标或空字符串直接返回。DOM 等同步分支返回 <code>void</code>,二进制转换分支返回 Promise;统一使用 <code>await print()</code> 可以捕获转换错误。成功发起打印流程无法确认纸张输出。</li></ul><div class="language-ts vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">ts</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { usePrint } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;@/hooks&#39;</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { ref } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">from</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &#39;vue&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">export</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> function</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> useReportPrint</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> report</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> ref</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">&lt;</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">HTMLElement</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">&gt;()</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> usePrint</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(report, {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> type: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;html&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> documentTitle: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&#39;库存报表&#39;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> })</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> { report, print }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><p>TablePro 内置打印按当前页和可见列生成报表。要保留自定义业务排版,参考 <a href="./../table-pro.html#打印与-csv">TablePro 打印</a>。</p><p>源码:<a href="https://github.com/XiaoDaiGua-Ray/ray-template/blob/403fdd589abbe37b38e1240d34f8daf2a60d3690/packages/composables/src/useDomToImage.ts" target="_blank" rel="noreferrer">useDomToImage</a> · <a href="https://github.com/XiaoDaiGua-Ray/ray-template/blob/403fdd589abbe37b38e1240d34f8daf2a60d3690/packages/composables/src/usePrint.ts" target="_blank" rel="noreferrer">usePrint</a></p>`,12)])])}const E=i(n,[["render",p]]);export{c as __pageData,E as default};