Rework Apple printing to better handle rotation

Apparently, mixed orientation isn't a thing in normal Apple print but
can be handled by the PDFKit printing
This commit is contained in:
Seth Hillbrand 2025-08-17 07:07:29 -07:00
parent 98a8f8678a
commit 35ac6bdeb4

View File

@ -26,6 +26,29 @@ namespace KIPLATFORM
{ {
namespace PRINTING namespace PRINTING
{ {
BOOL hasMultipleOrientations( PDFDocument* document )
{
if( [document pageCount] == 0 )
return NO;
PDFPage* firstPage = [document pageAtIndex:0];
NSRect firstBounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox];
BOOL firstIsLandscape = firstBounds.size.width > firstBounds.size.height;
for( NSUInteger i = 1; i < [document pageCount]; i++ )
{
PDFPage* page = [document pageAtIndex:i];
NSRect bounds = [page boundsForBox:kPDFDisplayBoxMediaBox];
BOOL isLandscape = bounds.size.width > bounds.size.height;
if( isLandscape != firstIsLandscape )
return YES;
}
return NO;
}
PRINT_RESULT PrintPDF( const std::string& aFile, bool fit_to_page) PRINT_RESULT PrintPDF( const std::string& aFile, bool fit_to_page)
{ {
@autoreleasepool @autoreleasepool
@ -44,17 +67,35 @@ PRINT_RESULT PrintPDF( const std::string& aFile, bool fit_to_page)
return PRINT_RESULT::FAILED_TO_LOAD; return PRINT_RESULT::FAILED_TO_LOAD;
} }
NSPrintInfo* printInfo = [NSPrintInfo sharedPrintInfo]; PDFView* pdfView = [[PDFView alloc] init];
[pdfView setDocument:document];
PDFPrintScalingMode scalingMode = fit_to_page ? NSPrintInfo* printInfo = [[NSPrintInfo sharedPrintInfo] copy];
kPDFPrintPageScaleDownToFit : kPDFPrintPageScaleNone;
NSPrintOperation* op = [document printOperationForPrintInfo:printInfo BOOL hasMixed = hasMultipleOrientations( document );
scalingMode:scalingMode
autoRotate:YES]; if( hasMixed )
{
[printInfo setOrientation:NSPrintingOrientationPortrait];
[printInfo setHorizontallyCentered:YES];
[printInfo setVerticallyCentered:YES];
}
if( fit_to_page )
{
NSMutableDictionary* settings = [[printInfo printSettings] mutableCopy];
[settings setObject:@YES forKey:@"com.apple.print.PrintSettings.PMScaleToFit"];
[printInfo setPrintSettings:settings];
[settings release];
}
NSPrintOperation* op = [NSPrintOperation printOperationWithView:pdfView printInfo:printInfo];
[printInfo release];
if( !op ) if( !op )
{ {
[pdfView release];
[document release]; [document release];
return PRINT_RESULT::FAILED_TO_PRINT; return PRINT_RESULT::FAILED_TO_PRINT;
} }
@ -66,22 +107,24 @@ PRINT_RESULT PrintPDF( const std::string& aFile, bool fit_to_page)
PRINT_RESULT result; PRINT_RESULT result;
if (success) if( success )
{ {
result = PRINT_RESULT::OK; result = PRINT_RESULT::OK;
} else } else
{ {
// Check if operation was cancelled (uncertain that this works)
NSPrintInfo* info = [op printInfo]; NSPrintInfo* info = [op printInfo];
NSDictionary* settings = [info printSettings]; NSDictionary* settings = [info printSettings];
if ([[settings objectForKey:NSPrintJobDisposition] isEqualToString:NSPrintCancelJob]) { if( [[settings objectForKey:NSPrintJobDisposition] isEqualToString:NSPrintCancelJob] )
{
result = PRINT_RESULT::CANCELLED; result = PRINT_RESULT::CANCELLED;
} else { } else
{
result = PRINT_RESULT::FAILED_TO_PRINT; result = PRINT_RESULT::FAILED_TO_PRINT;
} }
} }
[pdfView release];
[document release]; [document release];
return result; return result;
} }