> Is it possible to add a function to output this as PDF or save the result as PNG?
Fortunately, in Observable, every cell that contains SVG or canvas graphics has image export built in. Just click the dotted menu to the left of the cell, and it will offer you downloads in either SVG or PNG flavors.