fixed some errors in writePDF function