如何使用 IronPrint 结合 Google Antigravity

Curtis Chau
Curtis Chau
更新: 2026年10月26日

酒店前台为每位到达的客人打印签字入住卡。 文员在浏览器中点击打印,但操作从 Web 服务器发出,因此各项发送到错误的打印机或纸张上仍将返回成功消息。 这些问题不会出现在构建日志中。

Google Antigravity 可以像文员一样查看页面。 其 /browser 命令启动一个沙箱的 Chrome 子代理,可以点击每个打印按钮并截图其结果。 IronPrint 从服务器将卡片 PDF 用 Printer.PrintAsync 发送到打印机,此方法在 IronPrint 的ASP.NET Web 应用指南中有介绍。 下面的步骤将同时使用这两个工具来构建页面,并针对六个已知结果的到达进行测试。

目标受众是那些其 AI 代理编写从 Web 服务器打印代码的 .NET 开发者。 选择打印机硬件并配置打印队列已经超出了范围。

快速入门
NuGet使用NuGet安装

PM > Install-Package IronPrint

通过在 NuGet 包管理器控制台中运行上述命令安装 IronPrint,或在 NuGet 包管理器中搜索该包。

打开 Antigravity 中的 FrontDesk 项目并描述到达页面。

/plan Add an /Arrivals page to the FrontDesk .NET 9 MVC site.
Each row's Print registration card button prints that card from the server with IronPrint.

Set up the project first:

- Add the IronPrint package, version 2026.10.1. Its namespace is IronPrint.
- Target net9.0-windows.

The page works like this:

- It lists today's guests from App_Data/arrivals.csv.
- Each card is App_Data/cards/<ReservationId>.pdf.
- The Print action awaits Printer.PrintAsync, never Wait or Result.
- Cards print on A5, two copies, grayscale, with PaperOrientation.Automatic.
- Check the printer name in Printing:PrinterName against Printer.GetPrinterNames before any job.
- Refuse a missing card, or a file that doesn't start with %PDF-, before IronPrint opens it.
- Each click shows its outcome on the page.
- Add the page to the nav bar.

Every printer here opens a Save As dialog. Send no real job.
With Printing:DryRunFolder set, write each job to a JSON file there.
The file holds the reservation, card path, printer and every setting.
appsettings.Development.json already sets the folder.

At startup, set IronPrint.License.LicenseKey once from IRONPRINT_LICENSE_KEY.
The app reads the variable itself, so you never need the value.
Check only that it's set and its length.
Don't print, log or save the key, or list the environment variables.

Answer three questions in the Implementation Plan:

- What does IronPrint do with a printer name it can't find?
- How does the name check compare names?
- What does the page show when IronPrint throws?

Stop when dotnet build reports zero errors.
Text

成功构建不会测试三个答案,因此在批准计划前要检查每一个。

IronPrint 不能读取 IRONPRINT_LICENSE_KEY。 在我们的测试中,该变量被设置,直到 License.LicenseKey 分配为止 License.IsLicensed 都保持为 false。 许可证密钥指南 涵盖其他选项,如 appsettings.json 和 Web.config。



什么是Google Antigravity,它与聊天助手有何不同?

在 Google's 概述 中,桌面 Antigravity App 是一个独立程序,开发者可以在其中启动、协调和检查自主代理团队。 当聊天助手用文本回复时,Antigravity 的代理生成工件,如实施计划、演练和截图。

一种从记忆中命名包的代理程序可以安装由攻击者发布的相似软件包,这种方法被称为滥用。 在任何添加依赖项的提示中给出 IronPrint 包名和版本。 IronPrint 的文档文件列在AI 编码助手指南中,该指南还有一个单提示验收测试。

您应该在计划、代码更改和演练中检查什么?

预期 IronPrint 拒绝未知打印机名称的计划忽略了名称检查存在的原因。 在代码更改中,找到打印机名称比较并查找每个 IronPrint 成员在 API 参考 中。

在 演练 中预期每次点击有一个截图。 桌面应用将每个截图保存为开放评论的图像工件。 截图上的错误信息评论告诉代理哪个行失败了,无需新的提示。

如何在实际浏览器中测试打印按钮?

/browser 提示要求一个表格,只能通过点击每个按钮填写。

/browser Start the site in the background with dotnet run and go to /Arrivals.
The http profile serves it on http://localhost:5318.

Click Print registration card on every row, top to bottom:

1. Click the button.
2. Wait for the outcome message.
3. Take a screenshot.

TestData/expected-jobs.csv says what each click should produce.
A row marked job should leave one new JSON file in print-jobs.
A row marked refused should leave no file, and the page should say why.

Sum up the clicks in a Walkthrough table with these columns:

- reservation
- expected outcome
- message on the page
- job file written
- paper size, orientation, copies and grayscale from that file
- pass or fail

Before you touch the code, find the log line or exception behind each failed row.
Write it under the table, or say nothing was logged.
Fix the cause, restart the site and click every row again.
Stop that dotnet process by its PID at the end.
Text

在六次到达中,两次有单页 A5 卡片,第三次有第二页的房屋规则。 一个团体预订有一张 A4 横向卡片,一个预订没有卡片,而一张卡片文件包含的不是 PDF,而是导出失败的文本。

前台到达页面列出了六个预订,每个行上都有状态和打印注册卡按钮

/Arrivals 页面在预订1047的点击之后,这是代理最终演练中的七个截图之一。

请注意: 首先阅读预订1045和1047的行。两者都必须由页面本身拒绝。

打印请求到达哪个打印机?

IronPrint 2026.10.1 不验证打印机名称。 我们使用未知名称、adobe pdf小写书写且无名称构建了作业。 IronPrint 将所有三份地址都发送到 Windows 的默认打印机,没有例外。 将任何字符串分配给 PrintSettings.PrinterName 也能成功。 因此配置中的输入错误将在服务器的默认打印机上打印卡片。

在测试机上,默认打印机是 Microsoft Print to PDF,它会在“另存为”对话框中请求文件名。 ASP.NET 指南中的高级示例将其作业发送到同一个打印机。 Web 服务器上的用户没有回答对话框,在探测中,只有在有人输入文件名后,作业才会完成。

在每个作业之前将配置名称与 Printer.GetPrinterNames 进行比较可以防止这两种结果。 比较必须是逐字的,因为 IronPrint 完全匹配队列名称,包括大小写。

您如何设置Antigravity可以工作的项目?

给代理一个已经编译并提供页面的存储库。 dotnet new mvc -f net9.0 生成的 FrontDesk 项目,在第一次提示前一次完成构建。 其在 Properties/launchSettings.json 中的 http 启动配置文件监听 5318 端口,/browser 提示打开。

目标 net9.0-windows。 一个为普通 net9.0 构建的控制台探测编译时没有警告,但报告了不同的纸盘名称,例如对于 Adobe PDF 打印机为“自动选择”而不是“AutoSelect”,并因缺少文件而提高 PlatformNotSupportedException。

Which IronPrint version does FrontDesk.csproj reference? Quote the PackageReference line.
Then tell me where the code applies the key.
Read the code for this, and don't start the app.

Check the project against these rules for the key:

- It reaches the app only through IRONPRINT_LICENSE_KEY at startup.
- No appsettings file has an IronPrint.LicenseKey entry.
- Properties/launchSettings.json holds no key.
- With the variable missing, startup writes a single warning naming the variable, with no value.

Change Program.cs only if that warning isn't there.
Don't edit anything else.
Text

没有许可证密钥,IronPrint 2026.10.1 列出了已安装的四个打印机及其纸盘。 Printer.PrintAsync 在读取文件前因“需要生产许可证”抛出 LicensingException,因此打印队列未收到任何作业。 在测试页面上,第一个未授权点击在 998 毫秒内返回该消息,后续点击在 19 至 30 毫秒内返回。 异常类型不是公开的,并且 catch (LicensingException) 编译失败,出现错误 CS0122。

如何将页面规则应用于后续任务?

每行通过后,将会话的决定保存为规则。 Antigravity 的规则文档表示代理加载路径中每个文件夹下的 AGENTS.md 或 GEMINI.md 文件,从根目录至其读取或编辑的文件。 /learn 根据会话中的更正提出规则和技能,只有在批准后才写入文件。

/learn Write the decisions of this session to a new AGENTS.md in the repository root.
Use the heading "Front desk printing" and cover:

- the target framework IronPrint needs here
- how the printer name is checked, and why
- the settings every registration card prints with
- the dry-run folder, and that no real job goes out from this machine
- where IRONPRINT_LICENSE_KEY comes from, and that it never goes into a file
- that a change is finished only when every row of TestData/expected-jobs.csv passes in /browser

Tell me the rules before you save them.
Text

如何从页面排队打印作业?

ASP.NET 指南建议用于大批量打印的队列。

/plan Show me the Implementation Plan Artifact and wait for my go-ahead before editing.

Move the printing code out of the controller into Services/CardPrinter.cs.
Register it with dependency injection.

Here's how CardPrinter behaves:

- BuildSettings returns the PrintSettings for a card, so tests can read them without printing.
- A printer name passes only on an exact match with Printer.GetPrinterNames, case included.
- A missing card, a non-PDF file or an unknown printer comes back as a refusal with a reason.
- IronPrint's LicensingException isn't public, so match it by type name.
- That exception becomes a message saying the key is missing.
- With Printing:DryRunFolder set, it still writes the JSON job file and sends nothing.

Then add a Print all cards button to /Arrivals.
The new button works this way:

- It puts one job per arrival on a Channel that a BackgroundService reads.
- Jobs run one at a time.
- The request returns straight away.
- The page lists each job as queued, done or refused.

Finish with the /browser table again, then click Print all cards and add its rows.
Wait until no job shows as queued before that last screenshot.
Text

提示要求一个接近此示例的服务,该示例针对 IronPrint 2026.10.1 进行编译。

using IronPrint;

public sealed record PrintOutcome(bool Printed, string Message);

public sealed class CardPrinter(IConfiguration config, IWebHostEnvironment env)
{
    public static PrintSettings BuildSettings(string printerName) => new()
    {
        PrinterName = printerName,
        PaperSize = PaperSize.A5,
        PaperOrientation = PaperOrientation.Automatic,
        NumberOfCopies = 2,
        Grayscale = true,
    };

    public async Task<PrintOutcome> PrintCardAsync(string reservationId)
    {
        string card = Path.Combine(env.ContentRootPath, "App_Data", "cards", reservationId + ".pdf");
        if (!File.Exists(card))
            return new(false, $"No registration card for reservation {reservationId}.");

        var header = new byte[5];
        using (var stream = File.OpenRead(card))
            stream.ReadExactly(header);
        if (System.Text.Encoding.ASCII.GetString(header) != "%PDF-")
            return new(false, $"Card {reservationId} is not a PDF.");

        // IronPrint sends a job for an unknown name to the Windows default printer, so match the name here.
        string printer = config["Printing:PrinterName"] ?? "";
        List<string> installed = await Printer.GetPrinterNamesAsync();
        if (!installed.Contains(printer, StringComparer.Ordinal))
            return new(false, $"Printer '{printer}' is not installed on this server.");

        try
        {
            await Printer.PrintAsync(card, BuildSettings(printer));
            return new(true, $"Card {reservationId} sent to {printer}.");
        }
        catch (Exception ex) when (ex.GetType().Name == "LicensingException")
        {
            return new(false, "IronPrint has no license key. Set IRONPRINT_LICENSE_KEY on the server.");
        }
    }
}
C#

该示例省略了干运行分支。 没有通过标头检查传递给 IronPrint,1047 预订的卡片抛出了“不支持的文件类型”的 InvalidDataException。

前台打印每张卡片收到什么?

测试机的四个打印机没有任何一个可以无人值守地完成作业。 因此,记录的每个作业都使用 IronPrint 的内部作业生成器重新构建,它的页面和打印票被编写到一个 XPS 文件而不是发送到队列。

预订卡片文件作业中的页面发送到打印机的页面
1042A5 纵向,1 页1A5 纵向,148 x 210 mm
1043A5 纵向,2 页2A5 纵向,148 x 210 mm
1044A4 横向,1 页1A5 横向,210 x 148 mm
1045None0被页面拒绝
1046A5 纵向,1 页1A5 纵向,148 x 210 mm
1047文本,不是 PDF0被页面拒绝

PaperOrientation.Automatic 将组卡片转成横向 A5 页面并缩放以适应。 使用 PaperOrientation.Portrait,同样的卡片被缩小到纵向页面的宽度并留下 107.9 mm 的空白。 使用默认 PaperSize.PrinterDefault,A5 卡片被放大以填满打印机的默认 A4 页面。

NumberOfCopies 是打印票中的请求,作业包含每页一次。 Grayscale 将 PDF 页面渲染成灰色并将票的颜色设置为单色。 将 Dpi 从 300 降到 150 将一个卡片的模拟作业从 170,257 字节减至 74,526 字节。

预订 1042 和 1043 的输入卡片来自 App_Data/cards。

输出

下面的两个 PDF 包含了那些卡片最终应用程序的模拟作业的页面图像,未更改并以每个打印票中的页面尺寸。

注册卡 1042 是 IronPrint 构建作业时的情形,一个 A5 纵向页面以 300 DPI 捕获到文件代替打印机。

注册卡 1043 是 IronPrint 构建作业时的情形,两个 A5 纵向页面以 300 DPI 捕获到文件代替打印机。

何时应指派多个代理?

为无需浏览器的检查添加第二个代理。 在这里,一个代理测试代码中的设置,而另一个重复文员的点击。

Agent A: create an xUnit project called FrontDesk.Tests on net9.0-windows, like the web project.
Its tests cover CardPrinter and never send a job:

1. For each job row in TestData/expected-jobs.csv, check the settings BuildSettings returns.
2. Check that reservations 1045 and 1047 are refused, and why.
3. Check that "adobe pdf" in lower case is refused as a printer name.

FrontDesk.csproj is in the repository root, so exclude FrontDesk.Tests from its compile items.
Run them with dotnet test FrontDesk.Tests.

Agent B: wait for Agent A's tests to pass on the current code.
Then use /browser to click every Print button and Print all cards.
Add one screenshot per click to the Walkthrough, and mention there that the tests ran first.
Text
请注意: 桌面应用可以运行代理 A 和代理 B 作为独立的代理会话。 在合并前并排阅读两个演练。

如何将页面部署到 IIS?

IronPrint 的 NuGet 页面未列出 Linux 支持,只有 Windows、macOS、iOS 和 Android,因此前台服务器运行 Windows。 ASP.NET 指南补充指出应用程序池身份需要访问打印机。

This app will run under IIS on a Windows Server 2022 machine at the front desk.
Leave the C# alone.
IIS isn't installed here, so publish only.

Do these in order:

1. Run dotnet publish -c Release -r win-x64 --self-contained false -o out/iis.
2. List every native DLL in out/iis with the NuGet package that brought it in.
3. Confirm that web.config in out/iis carries no license key.

Then tell me where IIS should set IRONPRINT_LICENSE_KEY for this app.
Text

发布文件夹保存服务器接收到的,因此检查它不需要 IIS。 校准应用的 win-x64 发布包含 235 个文件,占 40 MB。 唯一的本机库是 IronPrintInterop.dll (9,926,400 字节) 和 qpdf.dll (2,802,432 字节),均来自 IronPrint 包。 生成的 web.config 未包含许可证密钥。 IIS 10 可以通过应用程序池的环境变量传递密钥给工作进程。 从项目文件夹发布也从 print-jobs 复制了干运行作业文件到输出。

在实际运行中这些提示生成了什么?

所有七个提示在一个 Antigravity CLI 对话中无人看管运行,跳过了每个权限提示。 许可证密钥的唯一来源是 IRONPRINT_LICENSE_KEY。 每个浏览器通行都作为干运行运行,打印队列始终为空。 每行也在第一次提示和最终应用程序后独立单击。

  • 在第一个提示之前,项目目标是 net9.0 并且未引用任何 NuGet 包。
  • 在第一个提示后,目标是 net9.0-windows 并引用 IronPrint 2026.10.1。
  • 实施计划表明,IronPrint 为未知的打印机名发送作业到默认打印机,如我们所测量。
  • 第一个提示的代码等待读取 Printer.PrintAsync 而没有调用 Wait 或 Result。
  • 页面拒绝预订 1045 和 1047 并同时记录了其他四个的作业。
  • 每个记录的作业请求 A5 纸张,两份灰度和自动方向。
  • 四个模拟作业容纳了 5 页 A5 页面,组卡的页面是横向。
  • 打印所有卡片在 2 秒内完成所有六次到达,四份作业完成,二份拒绝。
  • 每个提示都留下 0 错误的构建。
  • 最终应用程序在发布中构建时 0 警告。
  • 在没有密钥的情况下启动,最终应用程序记录了一个命名 IRONPRINT_LICENSE_KEY 的警告。
  • 所有四个 xUnit 测试在没有许可证密钥的情况下通过。
  • 运行的 win-x64 发布持有相同的两个本机库和一个没有密钥的 web.config。
  • 有些步骤与页面中的运行不一样。 Antigravity 的 /browser 子代理未能连接到 Chrome,因此在每个浏览器通行中代理通过其自有 Puppeteer 脚本控制 Chrome。 两个计划优先的提示在计划后等待批准。 /learn 在工件中提出了六个点,并未写入 AGENTS.md。 在双代理提示中,主会话运行代理 A 的测试并且在代理 B 子代理失败后自己点击行。 代理在存储库根目录留下面 Chrome 日志、Cookie 文件和截图。

ChatGPT Codex 将其工作分为探险者和工人子代理。 在GitHub Copilot中,批处理作业也有运行日志记录每个表单设置且不设打印调用的干运行。 Antigravity 版本、型号、运行时间和测试机在共享的运行记录中。


故障排除


有问题吗?

如果您有任何问题,请联系support@ironsoftware.com

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

NuGet 下载量 52,106版本:2026.11刚刚发布