NSPrintOperation PDFOperationWithView

Folks;


macOS 10.15 - Xcode 11.3.1

I've been somewhat successful getting this method to create a PDFDocument from the contents of an NSTextView.


However, the contents of this NSTextView is an NSAttributedString (set using TextStorage).

The view behaves as expected and there is .pdf document created.


The contents of the .pdf file appears to be the .string value of the NSTextView.


How do I use this NSPrintOperation to produce a PDFDocument which retains the attributed string?


Thanks for any thoughts!

Steve


BTW: If anyone has a means of producing a PDFDocument from NSAttributedString for macOS that does not involve NSPrintOperation I would appreciate a quick example or a link... I used to use 'cupsfilter' but it has been marked deprecated AND it also yields this same result... It logs

'cupsfilter: No filter to convert from text/rtf to application/pdf.' but creates the file...(but loses the attributed string)

Answered by SwampDog in 409486022

If anyone is still following along...

Thanks go to janabanan for staying with this thread!!


Here's simplified code which produces a .pdf file which retains the links embedded in the NSAttributedString which is displayed in 'sourceTextView'. This .pdf file is also appropriately paginated.


[savePanel beginSheetModalForWindow:self.windowController.window completionHandler:^(NSInteger result){

if (result == NSFileHandlingPanelOKButton) {

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];

[printInfo setHorizontalPagination:NSFitPagination];

[printInfo setVerticallyCentered:NO];

[printInfo setHorizontallyCentered:NO];

[printInfo setLeftMargin:67.0];

[printInfo setRightMargin:67.0];

[printInfo setTopMargin:72.0];

[printInfo setBottomMargin:72.0];

[printInfo dictionary] [NSPrintJobDisposition] = NSPrintSaveJob;

[printInfo dictionary] [NSPrintJobSavingURL] = [savePanel URL];

NSPrintOperation *op = [NSPrintOperation printOperationWithView:self.windowController.sourceSourceTextView printInfo:printInfo];

[op setShowsPrintPanel:NO];

[op setShowsProgressPanel:NO];

[op runOperation];

}

[savePanel orderOut:self];

}];

janabanana


So the ONLY difference here is I am NOT using NSDocument.


Why would that make any difference?

It shouldn't make a difference. An attributed string is an attributed string. Send me an email and I will send you a link to download my test code which shows you how to do it both from an NSDocument class and from the WindowController class. Both tested and working as expected.

Accepted Answer

If anyone is still following along...

Thanks go to janabanan for staying with this thread!!


Here's simplified code which produces a .pdf file which retains the links embedded in the NSAttributedString which is displayed in 'sourceTextView'. This .pdf file is also appropriately paginated.


[savePanel beginSheetModalForWindow:self.windowController.window completionHandler:^(NSInteger result){

if (result == NSFileHandlingPanelOKButton) {

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];

[printInfo setHorizontalPagination:NSFitPagination];

[printInfo setVerticallyCentered:NO];

[printInfo setHorizontallyCentered:NO];

[printInfo setLeftMargin:67.0];

[printInfo setRightMargin:67.0];

[printInfo setTopMargin:72.0];

[printInfo setBottomMargin:72.0];

[printInfo dictionary] [NSPrintJobDisposition] = NSPrintSaveJob;

[printInfo dictionary] [NSPrintJobSavingURL] = [savePanel URL];

NSPrintOperation *op = [NSPrintOperation printOperationWithView:self.windowController.sourceSourceTextView printInfo:printInfo];

[op setShowsPrintPanel:NO];

[op setShowsProgressPanel:NO];

[op runOperation];

}

[savePanel orderOut:self];

}];

NSPrintOperation PDFOperationWithView
 
 
Q