如何在 Google Antigravity 中使用 IronPrint

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

酒店櫃台為每位到達的客人打印一張登記卡,以便在櫃檯上簽名。 店員在瀏覽器中點擊打印,但工作從網頁伺服器發出,因而發送到錯誤打印機或錯誤紙張大小的卡片仍然返回成功訊息。 這些問題不會出現在構建日誌中。

Google Antigravity 可以像店員一樣審閱頁面。 其 /browser 命令啟動一個沙盒化的 Chrome 子代理,可以點擊每個打印按鈕並截取結果的截圖。 IronPrint 使用 Printer.PrintAsync從伺服器向打印機發送卡片 PDF,這種方法在 IronPrint 的ASP.NET 網路應用指南中描述。 以下步驟使用這兩個工具構建頁面,然後針對六個已知結果的到達進行測試。

.NET 開發者是目標觀眾,他們的 AI 代理會撰寫從網頁伺服器打印的代碼。 選擇打印機硬體和配置列印佇列不在範圍內。

快速開始
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.IsLicensed 保持為 false 直到代碼分配了 License.LicenseKey。 許可金鑰指南包括其他選項,如 appsettings.json 和 Web.config。



Google Antigravity是什麼,它與聊天助手有何不同?

在 Google 的總覽中,桌面 Antigravity 應用是獨立程序,開發人員在其中進行發起、協調和檢查自主代理團隊。 在聊天助手以文本回答的地方,Antigravity 的代理產生藝術品如實施計劃、說明書和截圖。

一個能從記憶中命名包的代理可以安裝由攻擊者發佈的類似物,這種技術稱為slopsquatting。 在任何增加依賴項的提示中提供 IronPrint 包名稱和版本。 IronPrint 對代理的文檔文件在AI 代碼助手指南中列出,其中還有一個提示驗收測試。

在計劃、代碼變更和流程演示中,您應該檢查些什麼?

期望 IronPrint 拒絕未知打印機名稱的計劃忽略了名稱檢查存在的原因。 在代碼更改中,找到打印機名稱比較,並在API 參考中查找每個 IronPrint 成員。

說明書中每次點擊預期一個截圖。 桌面應用保存每個截圖作為圖像藝術品打開供評論 在錯誤訊息的截圖上的評論告訴代理哪一行失敗,且不需要新的提示。

如何在真實瀏覽器中測試打印按鈕?

/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。

FrontDesk 到達頁面列出了六個訂位,每行都有一個狀態和一個打印登記卡按鈕

/Arrivals 頁面在點擊 1047 號預訂後顯示,這是代理最終說明書中的七個截圖之一。

請注意: 首先讀取 1045 和 1047 號預訂的行。它們都必須被頁面本身拒絕。

打印請求到達哪個打印機?

IronPrint 2026.10.1 不驗證打印機名稱。 我們構建了用未知名稱的作業,其中 adobe pdf 用小寫寫出且沒有名稱。 IronPrint 將這三個都發送到 Windows 默認打印機,沒有例外。 將任何字符串分配給 PrintSettings.PrinterName 也會成功。 配置中的打字錯誤因此會將卡片打印到服務器的默認打印機上。

在測試機上,默認打印機是 Microsoft Print to PDF,該打印機在“另存為”對話框中要求文件名。 ASP.NET 指南中的高級示例將其作業發送到同一打印機。 網頁伺服器上的用戶無法回答對話框,在探針中,向該打印機發送的作業僅在有人鍵入文件名後完成。

在每次工作之前將設定的名稱與 Printer.GetPrinterNames 比較可以防止這兩種結果。 比較必須是序數的,因為 IronPrint 完全匹配佇列名,包括大小寫。

如何設置一個Antigravity可以工作的專案?

給代理一個已經編譯且服務頁面的儲存庫。 dotnet new mvc -f net9.0 生成了 FrontDesk 項目,在第一次提示前進行了一次乾淨的構建。 其 http 啟動配置檔在 Properties/launchSettings.json 中以 5318 端口監聽,/browser 提示打開此端口。

目標 net9.0-windows。 為普通的 net9.0 構建的控制台探針編譯時無警告,但報告了不同的紙盤名稱,例如 Adobe PDF 打印機的“自動選擇”而不是“自動選擇”,並且缺少文件引發了 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 毫米的紙張空白。 使用默認 PaperSize.PrinterDefault,A5 卡片被放大以填滿打印機的默認 A4 頁面。

NumberOfCopies 是打印票據中的請求,工作包含每頁一次。 Grayscale 渲染 PDF 頁面為灰色,並將票據的顏色設為黑白。 將 Dpi 從 300 降至 150 將一張卡的模擬工作從 170,257 字節減少至 74,526 字節。

來自 App_Data/cards 的 1042 和 1043 號預約的輸入卡片。

輸出

下方的兩個 PDF 包含了那些卡片的最終應用模擬工作中的頁面圖像,未經更改並保持在打印票據中的頁面尺寸。

登記卡 1042 作為 IronPrint 構建的工作,300 DPI 下一個 A5 豎向頁面捕捉到文件中而不是打印機。

登記卡 1043 作為 IronPrint 構建的工作,300 DPI 下兩個 A5 豎向頁面捕捉到文件中而不是打印機。

何時啟動更多代理?

為不需要瀏覽器的檢查添加第二個代理。 此處一個代理在代碼中檢查設置,而另一個重複店員的點擊。

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。 唯一的原生庫是來自 IronPrint 套件的 IronPrintInterop.dll (9,926,400 字節) 和 qpdf.dll (2,802,432 字節)。 生成的 web.config 無許可金鑰。 IIS 10 可以透過應用程序池的環境變量傳遞金鑰給工作進程。 從項目文件發佈時,還將未完成作業的文件從 print-jobs 複製到輸出中。

在實際運行中,這些提示產生了什麼結果?

所有七個提示均在一個 Antigravity CLI 對話中無人監督運行,跳過所有權限提示。 許可金鑰的唯一來源是 IRONPRINT_LICENSE_KEY。 每次瀏覽器通過均作為模擬試運行,打印隊列保持空白。 每行亦在第一次提示之後和最終應用中獨立點擊。

  • 在第一次提示之前,項目以 net9.0 為目標,未引用任何 NuGet 套件。
  • 在第一次提示之後,其目標設定為 net9.0-windows,並引用了 IronPrint 2026.10.1。
  • 實施計劃中指出,IronPrint 會將未知打印機名稱的工作發送到默認打印機,如我們所測量的。
  • 第一次提示的代碼在沒有 Wait 或 Result 調用的情況下等待 Printer.PrintAsync。
  • 頁面拒絕了 1045 和 1047 號預留,並在兩次檢查中為其他四個記錄工作。
  • 每個記錄的工作要求 A5 紙張,兩份,灰度和自動方向。
  • 四個模擬工作含有五頁 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。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 52,106版本:2026.11剛剛發布