From 967056a4caed0e4a127efdc958cd236fa945d4eb Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Mon, 3 Nov 2014 15:44:24 +0800 Subject: [PATCH] Calculate page margins manually Based on implementation found at https://github.com/scottgarner/URL2PDF Fix #190. --- MacDown/Code/Document/MPDocument.m | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/MacDown/Code/Document/MPDocument.m b/MacDown/Code/Document/MPDocument.m index 8d9e4bb6..69cb6177 100644 --- a/MacDown/Code/Document/MPDocument.m +++ b/MacDown/Code/Document/MPDocument.m @@ -1048,11 +1048,30 @@ - (IBAction)exportPdf:(id)sender [panel beginSheetModalForWindow:w completionHandler:^(NSInteger result) { if (result != NSFileHandlingPanelOKButton) return; + + // Margin calculation based on https://github.com/scottgarner/URL2PDF NSPrintInfo *info = self.printInfo; + NSRect imageableBounds = info.imageablePageBounds; + NSSize paperSize = info.paperSize; + if (imageableBounds.size.width > paperSize.width) + { + imageableBounds.origin.x = 0; + imageableBounds.size.width = paperSize.width; + } + if (imageableBounds.size.height > paperSize.height) + { + imageableBounds.origin.y = 0; + imageableBounds.size.height = paperSize.height; + } [info.dictionary addEntriesFromDictionary:@{ NSPrintJobDisposition: NSPrintSaveJob, - NSPrintJobSavingURL: panel.URL + NSPrintJobSavingURL: panel.URL, + NSPrintBottomMargin: @(imageableBounds.origin.y / 2.0), + NSPrintTopMargin: @(imageableBounds.origin.y / 2.0), + NSPrintLeftMargin: @(imageableBounds.origin.x / 2.0), + NSPrintRightMargin: @(imageableBounds.origin.x / 2.0), }]; + NSView *view = self.preview.mainFrame.frameView.documentView; NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo:info];