Add guards to prevent write attempts to a closed pdf.File.
Perform minor cleanup.