// and positive is down and to the right. (0,0) is where the
// viewBox puts it.
gen.concatMatrix(sx, 0, 0, sy, fx, fy);
final boolean textAsShapes = false;
PSGraphics2D graphics = new PSGraphics2D(textAsShapes, gen);
graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
AffineTransform transform = new AffineTransform();
// scale to viewbox
transform.translate(fx, fy);
gen.getCurrentState().concatMatrix(transform);
if (paintAsBitmap) {
//Fallback solution: Paint to a BufferedImage
int resolution = (int)Math.round(context.getUserAgent().getTargetResolution());
RendererContextWrapper ctx = RendererContext.wrapRendererContext(context);
BufferedImage bi = paintToBufferedImage(painter, ctx, resolution, false, false);
float scale = PDFRenderer.NORMAL_PDF_RESOLUTION
/ context.getUserAgent().getTargetResolution();
graphics.drawImage(bi, new AffineTransform(scale, 0, 0, scale, 0, 0), null);
} else {
Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
painter.paint(graphics, area);
}