This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 打印
一个为 Android 和 iOS(AirPrint)提供打印功能的库。
expo-print 为 Android 和 iOS(AirPrint)提供打印功能的 API。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
API
import * as Print from 'expo-print';
Constants
Methods
Prints a document or HTML, on web this prints the HTML from the page.
Note: On iOS, printing from HTML source doesn't support local asset URLs (due to
WKWebViewlimitations). As a workaround you can use inlined base64-encoded strings. See this comment for more details.
Note: on iOS, when printing without providing a
PrintOptions.printerUrlthePromisewill be resolved once printing is started in the native print window and rejected if the window is closed without starting the print. On Android thePromisewill be resolved immediately after displaying the native print window and won't be rejected if the window is closed without starting the print.
Promise<void>Resolves to an empty Promise if printing started.
Prints HTML to PDF file and saves it to app's cache directory. On Web this method opens the print dialog.
Promise<FilePrintResult>Interfaces
Types
本地图片
在 iOS 上,从 HTML 源打印不支持本地资源 URL(由于 WKWebView 的限制)。替代方案是将图片转换为 base64 并内联到 HTML 中。
页面边距
在 iOS 上,你可以使用 margins 选项设置页面边距:
const { uri } = await Print.printToFileAsync({ html: 'This page is printed with margins', margins: { left: 20, top: 50, right: 20, bottom: 100, }, });
如果 useMarkupFormatter 被设置为 true,设置边距可能会导致打印输出末尾出现空白页。为防止这种情况,请确保你的 HTML 字符串是一个格式正确的文档,并在字符串开头包含 <!DOCTYPE html>。
在 Android 上,如果你在 printAsync 或 printToFileAsync 中使用 html 选项,生成的打印结果可能包含页面边距(这取决于 WebView 引擎)。
这些边距由 @page 样式块设置,你可以在 HTML 代码中覆盖它们:
<style> @page { margin: 20px; } </style>
更多详情请参见 MDN 上的 @page 文档。