Well, i had to do lot of trail and error kind of things before i write new comment with my observations on your code.
Still we are not comfortable with the solution you provided:

Reason :
I had kept your modified code and i did some modifications to reduce the time taking by pd4ml to generate the document.

Your code : ………………..
for ( int i = 0; i < 40; i++ ) { baos.reset(); int width = 900 + (i * 20); ............. Changed code : .................... for ( int i = 0; i < 3; i++ ) { baos.reset(); int width = 900 + (i * 425); ............. I just reduced the number of iterations. With either your code or my changed code , i could just expand the content to right side. So that left margin, right margin are in sync (i attached the output PDF). The problems i observed are : 1) I could just increase the space between columns. But text font still remain unreadable. How text was appearing before having this new code , same way text is appearing now. You can see in the attached pdf. We have to increase text font here. 2) I thought with the fix of text font ,we resolved the issue . But i just changed the input html to another huge html file comparitively. Problem still persists. Here content is not expanding. I see what ever fix we made is for 1st file. again if i change the numbers it starts expanding. Dont know what i should do with this behaviour of code. In one line , we might needed a code which should 1) expand the content to right , i mean left margins should be in sync 2) text font should be little visible 3)code should behave same for any input file irrespective of content.