With pixelDensity(2) enabled, PDF exports using offscreen buffers like beginRecord() and createGraphics(PDF) need to be scaled by 0.5 (1 / float(pixelDensity)) to have the expected result.
import processing.pdf.*;
void settings()
{
pixelDensity(2);
size(500, 500);
}
void setup()
{
beginRecord(PDF, "test.pdf");
//scale(1 / float(pixelDensity)); // uncomment to get expected pdf output
background(0, 255, 0);
rect(0, 0, width, height, 100, 100, 100, 100);
endRecord();
}
void draw() {}
void exportUsingPGraphics()
{
PGraphics pdf = createGraphics(width, height, PDF, "test_usingpgraphics.pdf");
pdf.beginDraw();
pdf.background(0, 0, 255);
//pdf.scale(1 / float(pixelDensity)); // uncomment to get expected pdf output
pdf.fill(255, 0, 0);
pdf.rect(0, 0, width, height, 100, 100, 100, 100);
pdf.dispose();
pdf.endDraw();
}
void keyReleased()
{
exportUsingPGraphics();
}