This is a very helpful script. Thank you for posting it.
I am getting several blank lines in the output. It occurs when the "class" column of table "database_permissions" is 6, which Books Online defines as "6 = Type". The output handles only classes 0 and 3 specifically, while all other classess go through the default code. Perhaps this section needs to be reviewed.