Skip to content

Commit 5036265

Browse files
committed
Fixed for newer versions of Sketch
1 parent 2c254bc commit 5036265

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

PDF Export.sketchplugin/Contents/Sketch/main.js

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function exportForOption(exportOption) {
5454

5555
var outputName = getOutputName(exportOption, artboards)
5656

57-
showOptionsWindow(exportOption, outputName, function() {
57+
showOptionsWindow(exportOption, outputName, function () {
5858
// Now that the user's preferences have been set, the ordering may need to change
5959
var orderedArtboards = exportableArtboards(exportOption, true)
6060

@@ -79,13 +79,13 @@ function exportableArtboards(exportOption, withOptions) {
7979

8080
switch (exportOption) {
8181
case exportOptions.allPages:
82-
doc.pages().forEach(function(page) {
82+
doc.pages().forEach(function (page) {
8383
if (withOptions && defaults.excludeWithPrefix && page.name().startsWith(defaults.exclusionPrefix)) {
8484
return
8585
}
8686

8787
var sortedPageArtboards = MSArtboardOrderSorting.sortArtboardsInDefaultOrder(page.artboards())
88-
sortedPageArtboards.forEach(function(artboard) {
88+
sortedPageArtboards.forEach(function (artboard) {
8989
artboards.push(artboard)
9090
})
9191
})
@@ -96,7 +96,7 @@ function exportableArtboards(exportOption, withOptions) {
9696
break
9797
case exportOptions.selection:
9898
var selectedArtboards = []
99-
selection.forEach(function(selectedLayer) {
99+
selection.forEach(function (selectedLayer) {
100100
if (selectedLayer.isMemberOfClass(MSArtboardGroup) || selectedLayer.isMemberOfClass(MSSymbolMaster)) {
101101
selectedArtboards.push(selectedLayer)
102102
}
@@ -146,7 +146,7 @@ function getOutputName(exportOption, artboards) {
146146
function filterArtboards(artboards) {
147147

148148
var filteredArtboards = []
149-
artboards.forEach(function(artboard) {
149+
artboards.forEach(function (artboard) {
150150

151151
// Omit artboards with prefix – when the option has been selected
152152
if (defaults.excludeWithPrefix && artboard.name().startsWith(defaults.exclusionPrefix)) {
@@ -178,7 +178,7 @@ function exportArtboards(artboards, outputName) {
178178
var pdf = PDFDocument.alloc().init()
179179
var filesToDelete = []
180180

181-
artboards.forEach(function(artboard) {
181+
artboards.forEach(function (artboard) {
182182

183183
if (defaults.exportToImages) {
184184
// Create a temporary image of the artboard
@@ -193,8 +193,18 @@ function exportArtboards(artboards, outputName) {
193193
newExportFormat.format = 'png'
194194

195195
var rect = artboard.absoluteRect().rect()
196-
var slice = MSExportRequest.exportRequestFromExportFormat_layer_inRect_useIDForName(newExportFormat, artboard, rect, false)
197-
doc.saveArtboardOrSlice_toFile(slice, imagePath)
196+
197+
198+
if (sketchVersionNumber() < 790) {
199+
var slice = MSExportRequest.exportRequestFromExportFormat_layer_inRect_useIDForName(newExportFormat, artboard, rect, false)
200+
doc.saveArtboardOrSlice_toFile(slice, imagePath)
201+
}
202+
else {
203+
var slice = MSExportRequest.exportRequestsFromExportableLayer_exportFormats_inRect_useIDForName(artboard, [newExportFormat], rect, false)
204+
doc.saveArtboardOrSlice_toFile(slice.firstObject(), imagePath)
205+
}
206+
207+
198208
filesToDelete.push(imagePath)
199209

200210
artboard.exportOptions().removeExportFormat(newExportFormat)
@@ -205,15 +215,37 @@ function exportArtboards(artboards, outputName) {
205215

206216
pdf.insertPage_atIndex(artboardPDF, pdf.pageCount())
207217
} else {
208-
var artboardPDF = MSPDFBookExporter.pdfFromArtboard(artboard)
209-
pdf.insertPage_atIndex(artboardPDF, pdf.pageCount())
218+
219+
220+
if (sketchVersionNumber() < 820) {
221+
var artboardPDF = MSPDFBookExporter.pdfFromArtboard(artboard)
222+
pdf.insertPage_atIndex(artboardPDF, pdf.pageCount())
223+
} else {
224+
225+
const options = {
226+
overwriting: true,
227+
'use-id-for-name': true,
228+
formats: ['pdf'],
229+
output: NSTemporaryDirectory()
230+
}
231+
232+
let exporter = MSSelfContainedHighLevelExporter.alloc().initWithOptions(options)
233+
exporter.exportLayers([artboard])
234+
235+
let path = NSTemporaryDirectory() + artboard.objectID() + '.pdf'
236+
let newPDF = PDFDocument.alloc().initWithURL(NSURL.fileURLWithPath(path))
237+
let page = newPDF.pageAtIndex(0)
238+
pdf.insertPage_atIndex(page, pdf.pageCount())
239+
240+
filesToDelete.push(path)
241+
}
210242
}
211243
})
212244

213245
// Save the whole PDF document — to the location the user specified
214246
pdf.writeToURL(saveLocation)
215247

216-
filesToDelete.forEach(function(file) {
248+
filesToDelete.forEach(function (file) {
217249
NSFileManager.defaultManager().removeItemAtPath_error(file, nil)
218250
})
219251
}
@@ -271,7 +303,7 @@ function alertNoArtboards(message) {
271303
function sketchVersionNumber() {
272304
var version = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString")
273305
var versionNumber = version.stringByReplacingOccurrencesOfString_withString(".", "") + ""
274-
while(versionNumber.length != 3) {
306+
while (versionNumber.length != 3) {
275307
versionNumber += "0"
276308
}
277309
return parseInt(versionNumber)

PDF Export.sketchplugin/Contents/Sketch/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Export artboards to PDF — from the current page, all pages or current selection",
44
"author": "David Williames",
55
"homepage": "https://github.com/DWilliames/PDF-export",
6-
"version": "2.1",
6+
"version": "2.2",
77
"identifier": "com.davidwilliames.sketch.pdf-export",
88
"appcast": "https://api.sketchpacks.com/v1/plugins/com.davidwilliames.sketch.pdf-export/appcast",
99
"compatibleVersion": "39",

0 commit comments

Comments
 (0)