Export PDF Sample

Description

Demonstrates how to export a Fusion 360 drawing document to PDF using the DrawingExportManager. Shows how to configure PDFExportOptions including sheet selection (all sheets, a specific range, or the current sheet), line weights, and range-based export.

Code Samples

import adsk.core, adsk.drawing, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        # The active document must be an open drawing document.
        doc = app.activeDocument
        drawingDoc = adsk.drawing.DrawingDocument.cast(doc)
        if not drawingDoc:
            ui.messageBox('A drawing document must be active to run this sample.')
            return
        drawing = drawingDoc.drawing

        # Get the DrawingExportManager for this drawing.
        exportMgr = drawing.exportManager

        # Ask the user where to save the PDF.
        fileDialog = ui.createFileDialog()
        fileDialog.title = 'Export PDF'
        fileDialog.filter = 'PDF Files (*.pdf)'
        dialogResult = fileDialog.showSave()
        if dialogResult == adsk.core.DialogResults.DialogOK:
            outputPath = fileDialog.filename
        else:
            return

        # Create PDFExportOptions — pass the output filename to createPDFExportOptions.
        pdfOptions = exportMgr.createPDFExportOptions(outputPath)

        # ── PDFExportOptions.sheetsToExport ───────────────────────────────────
        # Choose which sheets to include in the exported PDF.
        pdfOptions.sheetsToExport = adsk.drawing.PDFSheetsExport.AllPDFSheetsExport
        # Exports every sheet as a single multi-page PDF (default).

        # pdfOptions.sheetsToExport = adsk.drawing.PDFSheetsExport.CurrentPDFSheetExport
        # Exports only the sheet that is currently active in the UI.

        # pdfOptions.sheetsToExport = adsk.drawing.PDFSheetsExport.SelectedPDFSheetsExport
        # Exports all sheets currently selected in the UI (active sheet is always selected).

        # pdfOptions.sheetsToExport = adsk.drawing.PDFSheetsExport.RangePDFSheetsExport
        # Exports the range defined by sheetRange. Set sheetRange instead to auto-switch to this.

        # ── PDFExportOptions.sheetRange ───────────────────────────────────────
        # Only used when sheetsToExport is RangePDFSheetsExport.
        # Setting this property automatically switches sheetsToExport to RangePDFSheetsExport.
        # Accepts a range string such as '1-3' or '1-2,5'.
        # pdfOptions.sheetRange = '1-3'

        # ── PDFExportOptions.useLineWeights ───────────────────────────────────
        # When True, line weights defined in the drawing are respected in the PDF output.
        pdfOptions.useLineWeights = True
        # pdfOptions.useLineWeights = False

        # ── PDFExportOptions.openPDF ──────────────────────────────────────────
        # When True, the exported PDF is opened automatically after export completes.
        pdfOptions.openPDF = False
        # pdfOptions.openPDF = True

        # Execute the export.
        success = exportMgr.execute(pdfOptions)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))