PD4ML does not support JavaScript, so for the approach the measurement must be performed in a third-party tool (which adds yet another level of complexity).
You may compute a content right edge position using PD4ML API and an idle render() call:
pd4ml.setHtmlWidth(400);
pd4ml.render(url, byteArrayOutputStream);
System.out.println("right edge: " + (Long)pd4ml.getLastRenderInfo(PD4Constants.PD4ML_RIGHT_EDGE_PX));The returned right edge value (plus optionally some more margin pixels) can be used as
pd4ml.setHtmlWidth() parameter value for an effective
render() call.