Bit of a stream of consciousness below. I discovered after writing all that that I needed to switch width & height when setting values, and print landscape. That gets things to print as expected. But I really need to be able to set the Brother's notion of tape width in the Basic Options portion of the print panel.
I've got this Brother QL-800 USB label printer that takes spools of either die-cut labels, or continuous paper of a given width. It can cut between labels or the continuous paper at any point.
My app is intended to print small labels for small parts drawers. The app has a DB of parts, and I want to print dozens of unique labels of a specific height and width. Ideally, you'd set things up once, and then print one or more specific part labels without further UI interaction.
The NSPrintPanel is far too complex, and allows the user to ***** things up, so I'd like to forego it altogether.
I've written some simple code that does succeed in printing via the NSPrintPanel, but it’s positioning the text ultimately incorrectly.
I can't figure out how to properly size a view to print a label. I can get an NSPrintInfo
object that knows the label printer is selected (e.g. it shows the right name for NSPrintInfo.printer.name
). But it states the paperSize
is (612.0, 792.0), which if I assume 72 ppi, comes out to 8.5" x 11", which this paper clearly is not.
func
printLabel()
{
Task
{
let printInfo = NSPrintInfo()
let printer = printInfo.printer
print("Paper size: \(printInfo.paperSize)")
// printInfo.paperSize = CGSize(width: 47 / 25.4 * 72.0, height: 12.0 / 25.4 * 72.0)
printInfo.topMargin = 0.0
printInfo.bottomMargin = 0.0
printInfo.leftMargin = 0.0
printInfo.rightMargin = 0.0
printInfo.orientation = .landscape
printInfo.isHorizontallyCentered = false
printInfo.isVerticallyCentered = false
printInfo.scalingFactor = 1.0
let view = NSHostingView(rootView: createLabelPrintView())
view.frame.size = CGSize(width: 25 / 25.4 * 72.0, height: 12.0 / 25.4 * 72.0)
let op = NSPrintOperation(view: view, printInfo: printInfo)
op.showsPrintPanel = true
op.showsProgressPanel = true
op.printPanel.options.insert(.showsPaperSize)
op.printPanel.options.insert(.showsOrientation)
op.run()
}
}
@ViewBuilder
func
createLabelPrintView()
-> some View
{
VStack(spacing: 0)
{
Text("Line 1")
Text("Line 2")
}
.padding(0)
}
If I set the pageSize explicitly (uncommenting the line above),
The printer offers a long list of available sizes (although none I've found corresponds to the 0.47"/12 mm x continuous spool). By the way, it never remembers the size I selected, and always reverts to 8.5 x 11, which it will then refuse to print, hanging in Print Center with an error about mismatched page sizes.
I don’t know why the available page sizes have multiple sizes that don’t seem to match. E.g. 0.47" In the printer’s “Basic Options,” it shows the notion of a Width that's "not found," if I use a custom size:
I don't really understand where those printer options can be found and set.
Hmm, maybe I found a clue: I ran an NSPrintPanel by itself and examined the NSPrintInfo afterward:
let panel = NSPrintPanel()
panel.options.insert(.showsPaperSize)
panel.options.insert(.showsOrientation)
let result = await panel.beginSheet(using: printInfo, on: NSApplication.shared.windows.first!)
print("Result: \(result)")
print("Paper size: \(printInfo.paperSize)")
print("Settings: \(printInfo.printSettings)")
Paper size: (283.44000244140625, 82.08000183105469)
Settings: {
"AP_D_InputSlot" = "";
BrAutoTapeCut = ON;
BrBiDiPrint = ON;
BrBrightness = 0;
BrContrast = 0;
BrCutAtEnd = ON;
BrCutLabel = 1;
BrHalftonePattern = BrErrorDiffusion;
BrMargin = "3.0";
BrMultiColor = BrMultiColorMonochrome;
BrRedLevel = 0;
BrRemoveBlkSpace = OFF;
BrResolution = BrSpeed300x300dpi;
BrTapeLength = "69.1";
ColorModel = Gray;
Duplex = None;
PaperInfoIsSuggested = 0;
Resolution = 300x300dpi;
"com_apple_print_DialogDismissedBy" = Print;
"com_apple_print_DocumentTicket_PMSpoolFormat" = "application/pdf";
"com_apple_print_PDEsUsed" = "Printer Options";
"com_apple_print_PageToPaperMappingMediaName" = 29mm;
"com_apple_print_PageToPaperMappingType" = 1;
"com_apple_print_PrintSettings_PMCopies" = 1;
"com_apple_print_PrintSettings_PMCopyCollate" = 1;
"com_apple_print_PrintSettings_PMDestinationType" = 1;
"com_apple_print_PrintSettings_PMDuplexing" = 1;
"com_apple_print_PrintSettings_PMFirstPage" = 1;
"com_apple_print_PrintSettings_PMLastPage" = 2147483647;
"com_apple_print_PrintSettings_PMPageRange" = (
1,
2147483647
);
"com_apple_print_ticket_type" = "com.apple.print.PrintSettingsTicket";
"com_apple_print_totalPages" = 2147483647;
}
It has added a bunch of Br
-prefixed keys that clearly correspond to some of the options in the UI. But I'm not sure if I can set those programmatically and get things to print the way I expect. And I don't see anything for tape width.
How do I get an NSPrintPanel to show the preview without an NSPrintOperation? I want to react to any changes the user might make before things go to print.