#28589

Schematically it looks like that:
[language=java:2bwk47xo]double desiredRatio = (double)PD4Constants.LEDGER.width / PD4Constants.LEDGER.height;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for ( int i = 0; i < 40; i++ ) { int width = 900 + (i * 20); pd4ml.setHtmlWidth( width ); pd4ml.render(urls, baos); int height = ((Long)pd4ml.getLastRenderInfo(PD4Constants.PD4ML_DOCUMENT_HEIGHT_PX)).intValue(); // width = ((Long)pd4ml.getLastRenderInfo(PD4Constants.PD4ML_RIGHT_EDGE_PX)).intValue(); // for special cases double actualRatio = (double)width / height; if ( actualRatio < desiredRatio * 1.1 && actualRatio > desiredRatio * 0.9 ) {
break;
}

baos.reset();
}

fos.write( baos.toByteArray() );
fos.close();[/language:2bwk47xo]

The numeric constant values (width step, number of steps, matching aspect ratio scope) have no special meaning, so they needs to be adjusted for your particular case.

And please take into account, each render() call is a resource consuming task, so it makes sense to reduce a number of calls to the possible minimum.