Export PDF Sample

Description

Demonstrates exporting a Fusion drawing to PDF using Drawing.exportManager, DrawingExportManager.createPDFExportOptions, and execute. Configures PDFExportOptions for sheet selection (all, range, or current), line weights, and output filename.

Before running: A drawing document must be the active document (not a design). Open or create a drawing with at least one sheet, then run the script from Scripts and Add-Ins. If no drawing is active, the sample reports that a drawing document is required.

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()))