Print/save Strings in dynamo to .pdf in my device

Hey all,
is there a way I can print/save any string output of a node (information about my elements like parameter values in the project) to a .pdf file in a desired directory?
I know i can export these in excel using lists and stuff but i want a pdf instead of excel.

OR

can I convert an excel file to .pdf file using any node in a same script?

OR

can i integrate any string in views? i can print that view to .pdf later.

any of these can help me achieve my desired task
thanks in advance :slight_smile:

I don’t know that you can go directly to PDF from Dynamo, but you could push back to Revit (an assumption here based on the topic you picked) and make a PDF of that.

  1. Gather the string data
  2. Make a new drafting view
  3. Place a text note in the drafting view at the desired location
  4. Confirm the bounding box dimension of the text note is less than your desired paper size
  5. Create a PDF of the drafting view
2 Likes

thanks man, it working perfect :star_struck:

one problem; the string data is in lists, each text is getting place on the same place in the drafting view, any help?!


Easiest way would be to join the strings together before putting it on the view.
1st you can join them using separator as “\t” for tabs and the other time using “\n” to join all strings into one big text block.

Try this:


1 Like

wow… seems like it would work but i dont understand some of the nodes in here.
anyways im still not getting the exact thing.
have a look:
this is what i want:

this is what im getting:

Im using this graph. Din’t understand the x(0) code block so didnt use it :stuck_out_tongue: may be thats the problem? if yes please help how to make that code block. thanks :heart_eyes:

I think you will have to add a pad to the strings to make it even in length.
Can you share your Excel file?

1 Like

Yes. And you’ll have to do so evenly in each column.

  1. Transpose the data so you’re grouping stuff by column not by row.
  2. String.Length
  3. List.MaximumItem
  4. String.PadLeft or String.PadRight with a padding characters of " " and the count given by the count.
  5. Transpose the data again.
  6. String.Join with a tab character as the separator
  7. String.Join with a new line character as the separator

The result should be a bunch of text that reads like a table. Then proceed as before.

1 Like

I built a node that prints strings directly to pdf. It uses the built in Windows 10 “Microsoft Print to PDF” printer.

Here is the underlying python:

import clr
import System
import math

clr.AddReference('System.Drawing')

from System.Drawing.Printing import *
from System.Drawing import Brushes, Font, StringFormat, SizeF, StringAlignment

run = IN[0]
string = IN[1]
file = IN[2]
ltMargin = IN[3]
rtMargin = IN[4]
tpMargin = IN[5]
btmMargin = IN[6]
font = IN[7]
size = IN[8]

def printPDF():
	pd = PrintDocument()
	pd.PrinterSettings.PrinterName = "Microsoft Print to PDF"
	pd.PrinterSettings.PrintToFile = True
	pd.PrinterSettings.PrintFileName = file
	pd.DefaultPageSettings.Margins.Left = ltMargin
	pd.DefaultPageSettings.Margins.Right = rtMargin
	pd.DefaultPageSettings.Margins.Top = tpMargin
	pd.DefaultPageSettings.Margins.Bottom = btmMargin
	pd.PrintPage += PrintPageEventHandler(print_page)

	pd.Print()
	
#	return file

def print_page(sender, e):
	global string
	
	fnt = Font(font, size)
	
	charactersOnPage = clr.Reference[System.Int32]()
	linesPerPage = clr.Reference[System.Int32]()

	
	e.Graphics.MeasureString(string, fnt, SizeF(e.MarginBounds.Size), StringFormat.GenericDefault, charactersOnPage, linesPerPage)
	
	e.Graphics.DrawString(string, fnt, Brushes.Black, e.MarginBounds)
	
	string = string.Substring(charactersOnPage.Value)
	
	e.HasMorePages = string.Length > 0

if run:
	OUT = printPDF()
5 Likes

Yup, I tried the exact same thing. It works well in the node preview but messes up the layout when you write it to Revit. ("-" used for preview. Same thing happens with a " ")


Guessing it has something to do with the font and formatting.

1 Like

That’s a pretty nice node there. Good work!
Thanks for sharing the code.
It somehow shifts the 1st row

with these settings

Home.dyn (53.2 KB)
Book1.xlsx (15.8 KB)

1 Like

You’ll need to use a monospace font. An example of this would be “Courier New”.

2 Likes

That was the missing piece to this puzzle!
Thank you.


1 Like

guys im really grateful for your efforts :heart_eyes: :heart_eyes: :heart_eyes: :heart_eyes:

The Printed .pdf is perfect!!

but look at the drafting view in revit:

what could be the problem?!

this is my final graph:
Home.dyn (57.2 KB)

Wish I could help, but I’m not a Revit user. Maybe something to do with the font choice again?

1 Like

please help shah g…

You wanted a PDF which the code above gives you without Revit… what is the issue exactly?

You need to select a Text Note Type which has the Font set as “Courier New”

It does not exist by default so create it as shown below:

1 Like