https://github.com/cleancocoa/nstableview-printing
Example on how to print a NSTableView from your app but then also add text to the print-out.
https://github.com/cleancocoa/nstableview-printing
macos nsprintoperation nstableview printing
Last synced: about 2 months ago
JSON representation
Example on how to print a NSTableView from your app but then also add text to the print-out.
- Host: GitHub
- URL: https://github.com/cleancocoa/nstableview-printing
- Owner: CleanCocoa
- License: unlicense
- Created: 2021-06-12T08:39:29.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-29T15:45:24.000Z (over 3 years ago)
- Last Synced: 2025-06-20T10:11:42.579Z (4 months ago)
- Topics: macos, nsprintoperation, nstableview, printing
- Language: Swift
- Homepage:
- Size: 676 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NSTableView Printing Test
This is a demo project so you can check out how printing a table could work.
The goal here is to show tabular data on screen, and add text for print-outs.
## Discussion
`NSView`-based printing is a bit odd since it prints whatever `NSView.draw(_:)` produces. We can customzie that by overriding the `draw` method. But for a table view? Embedding a stock standard table view in a custom view hierarchy sounds more promising.
To add a multi-line label on page, we cannot use the stock view-based printing options since the label is not part of the view hierarchy. We need to set up our own view hierarchy with all the to-be-printed views.
For the simple page layout used here, I picked a vertical `NSStackView`. This can be printed via `NSPrintOperation(view: stackView)` and then showing the print dialog for that.
For complex floating layouts, a `NSTextView` with an attributed string and maybe even a custom `NSLayoutManager` might be necessary. That's how you could, in theory, get text wrapped around images or small tables like Pages does it.
Actually recommended by other devs: use `WKWebView` and HTML. WebKit does a great job with complex layouts; just look at Safari. Then you can export the web view's content as PDF, and then use the PDF data for the print dialog directly.
See my accompanying blog post for details and a recap:
## License
This code is in the Public Domain. Do what you want with it.