Export to PDF, Multiple Combined Assemblies Sheets to Multiple files

I am trying to use the Export to PDF to create combined PDF files with Assembly Name as the .pdf name for the Sheets within an Assembly list.

I have done this with
-creating temporary individual .pdf per sheet
-then combining with later with Merge.MergePDF’s
-then deleting the temporary individual .pdf once files are merged.

I would like to use Python to eliminate the secondary task of combining/merging .pdf files. I believe this would cut my processing time in half.

I have found the options that are within the Revit API, but looking for some help to get my to the finish line. I am fairly green in Python.

import clr

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# Current doc/app/ui
doc = DocumentManager.Instance.CurrentDBDocument

# Define list/unwrap list functions
def tolist(input):
    result = input if isinstance(input, list) else [input]
    return result

def uwlist(input):
    result = input if isinstance(input, list) else [input]
    return UnwrapElement(input)

# Preparing input from dynamo to revit
sheets  = uwlist(IN[0])
names   = IN[1]
direc   = IN[2]
options = IN[3]

#Sean Page, 2021
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

# Current document
doc = DocumentManager.Instance.CurrentDBDocument

# Collect inputs and set containers
Sheets = UnwrapElement(IN[0])
Names = IN[1]
Folder = IN[2]
Options = IN[3]

result = []
sheets = List[ElementId]()

# Collect and set export options
opts = PDFExportOptions()

opts.HideCropBoundaries = 1
opts.HideScopeBoxes = 1
opts.HideReferencePlane = 1
opts.HideUnreferencedViewTags = 1
opts.MaskCoincidentLines = 1
opts.Combine = 1
opts.FileName = names
opts.ColorDepth = ColorDepthType.GrayScale

# This will uses the Sheet Size for Paper size, or default to Letter
opts.PaperFormat = ExportPaperFormat.Default

# Execute the batch export
for sheet,name in zip(Sheets,Names):
	opts.FileName = names
	Sheet = List[ElementId]()
	Sheet.Add(sheet.Id)
	result.append(doc.Export(Folder,Sheet,opts))

# Preparing output to Dynamo
OUT = result

Just dropping a quick note that looks like you’re using an old version of my boilerplate with an error. The fixed one is below for uwlist:

def uwlist(input):
    result = input if isinstance(input, list) else [input]
    return UnwrapElement(result)

I used to have input instead of result on the last line accidentally. My bad!

You have some typos in your code also and your sheet list is actually a list of lists, try this:

# Define list/unwrap list functions
def tolist(input):
    result = input if isinstance(input, list) else [input]
    return result

def uwlist(input):
    result = input if isinstance(input, list) else [input]
    return UnwrapElement(result)

# Current document
doc = DocumentManager.Instance.CurrentDBDocument

# Collect inputs
sheetsList = uwlist(IN[0])
fileNames  = tolist(IN[1])
dirPath    = IN[2]

# Collect and set export options
opts = PDFExportOptions()
opts.HideCropBoundaries = 1
opts.HideScopeBoxes = 1
opts.HideReferencePlane = 1
opts.HideUnreferencedViewTags = 1
opts.MaskCoincidentLines = 1
opts.Combine = 1
opts.FileName = names
opts.ColorDepth = ColorDepthType.GrayScale
# This will uses the Sheet Size for Paper size, or default to Letter
opts.PaperFormat = ExportPaperFormat.Default

# Results list
results = []

# Execute the batch export
for sheets, n in zip(sheetsList, fileNames):
	# Set name
	opts.FileName = n
	# Make idList
	idList = List[ElementId]()
	# Build idList
	for s in sheets:
		idList.Add(s.Id)
	# Export
	result = doc.Export(dirPath, idList, opts)
	results.append(result)

# Preparing output to Dynamo
OUT = results

Gavin, Yes I was using your Export to PDF as a base Python example. Thank you for your YouTube videos that got me where I am now for Dynamo, now it is time to step my game up into Python. Your example gave me a better understanding of Python.

Below you can see what I am ending with,

Define list/unwrap list functions

def tolist(input):
result = input if isinstance(input, list) else [input]
return result

def uwlist(input):
result = input if isinstance(input, list) else [input]
return UnwrapElement(result)

Current document

doc = DocumentManager.Instance.CurrentDBDocument

Collect inputs

sheetsList = uwlist(IN[0])
fileNames = tolist(IN[1])
dirPath = IN[2]

Collect and set export options

opts = PDFExportOptions()
opts.HideCropBoundaries = 1
opts.HideScopeBoxes = 1
opts.HideReferencePlane = 1
opts.HideUnreferencedViewTags = 1
opts.MaskCoincidentLines = 1
opts.Combine = 1
opts.ColorDepth = ColorDepthType.GrayScale

This will uses the Sheet Size for Paper size, or default to Letter

opts.PaperFormat = ExportPaperFormat.Default

Results list

results =

Execute the batch export

for sheets, n in zip(sheetsList, fileNames):
# Set name
opts.FileName = n
# Make idList
idList = ListElementId
# Build idList
for s in sheets:
idList.Add(s.Id)
# Export
result = doc.Export(dirPath, idList, opts)
results.append(result)

Preparing output to Dynamo

OUT = [results, fileNames ,dirPath]

1 Like

Gavin,

Wondering if you can help me out again, I am now trying to send the PDF’s to different Directories.

I don’t know if it is possible, I have looked at other Python scripts but most are directed towards one Directory.

Made by Gavin Crump

Credit to Sean Page for sharing these methods via the forums!

Export PDF in Revit 2022 - Quick Example

Free for use

BIM Guru, www.bimguru.com.au

Boilerplate text

import clr

clr.AddReference(‘System’)
from System.Collections.Generic import List

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager

clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *

Current doc/app/ui

doc = DocumentManager.Instance.CurrentDBDocument

Define list/unwrap list functions

def tolist(input):
result = input if isinstance(input, list) else [input]
return result

def uwlist(input):
result = input if isinstance(input, list) else [input]
return UnwrapElement(result)

def todir(input):
result = input if isinstance(input, list) else [input]
return result

Current document

doc = DocumentManager.Instance.CurrentDBDocument

Collect inputs

sheetsList = uwlist(IN[0])
fileNames = tolist(IN[1])
dirPath = todir(IN[2])

Collect and set export options

opts = PDFExportOptions()
opts.HideCropBoundaries = 1
opts.HideScopeBoxes = 1
opts.HideReferencePlane = 1
opts.HideUnreferencedViewTags = 1
opts.MaskCoincidentLines = 1
opts.Combine = 1
opts.ColorDepth = ColorDepthType.GrayScale

This will uses the Sheet Size for Paper size, or default to Letter

opts.PaperFormat = ExportPaperFormat.Default

Results list

results =

Execute the batch export

for sheets, n in zip(sheetsList, fileNames):
# Set name
opts.FileName = n
# Make idList
idList = ListElementId
# Build idList
for s in sheets:
idList.Add(s.Id)
# Export
result = doc.Export(dirPath, idList, opts)
results.append(result)

Preparing output to Dynamo

OUT = [results, fileNames ,dirPath]

You will need to feed in directory paths per sheet list in that case versus just one.

The best way would be to introduce a zip iteration for each sheet list and directory path for it, versus a single iteration over sheets with a fixed directory path.

I am trying to combine those together the list of directory paths and filenames. But failing to see where I am falling short?

\xxx\files\Piece-Ticket-Files\11\11-1234\11-1234-ASP5001_prod.pdf
\xxx\files\Piece-Ticket-Files\11\11-1234\11-1234-ASP5002_prod.pdf
\xxx\files\Piece-Ticket-Files\13\13-1234.0\11-1234-C6001_prod.pdf
\xxx\files\Piece-Ticket-Files\13\13-1234.0\11-1234-C6002_prod.pdf

for sheets, n in zip(sheetsList, fileNames):
# Set name
opts.FileName = n
# Make idList
idList = ListElementId
# Build idList
for s in sheets:
idList.Add(s.Id)
# Make dirList
dpath = zip(dirPath, n)
# Export
result = doc.Export(dpath, idList, opts)
results.append(result)

You will need to provide a list of directory paths instead of just one to your node, then do it like this:

for sheets, n, d in zip(sheetsList, fileNames, dirPaths):
	# Set name
	opts.FileName = n
	# Make idList
	idList = ListElementId
	# Build idList
	for s in sheets:
		idList.Add(s.Id)
	# Export
	result = doc.Export(d, idList, opts)
	results.append(result)
1 Like

Gavin,

That worked! I learned about zip and other Python techniques today. I did a whole lot of research before trying to contacting you. Once again thank you!

image

1 Like