Issues exporting sketch to PDF: sketch gets cropped!

The issue is that I was using pixelDensity(2). See full solution to the problem here: java - Issues exporting Processing sketch to PDF: sketch gets cropped - Stack Overflow


Hi there!

I am trying to export my sketch to pdf. The issue that I have is that for some reason only a portion of my sketch is exported to pdf, as if the original sketch was cropped! When I run my sketch, 64 lines show up (as intended) and indeed when I save it to png all the 64 lines are there and sketch looks just the same as when I run it.

When I export the sketch to pdf though, only 16 line show up, as if the pdf was cropping my sketch and only the cropped portion was being exported.

This is the png showing what the sketch is supposed to look like:

This is what the pdf has exported

And this is my code:

import processing.pdf.*;
import java.util.Random;

int cols, rows;
int videoScale = 100;
boolean recordPDF = false;

void setup() {

  cols = width/videoScale;
  rows = height/videoScale;


void draw() {
  if (recordPDF) {
      beginRecord(PDF, "pdfs/" + str(random(1000)) + ".pdf");


  if (recordPDF) {
      recordPDF = false;
      println("Printed pdf.");

void keyPressed() {
    if (key == 'p') {
        recordPDF = true;
    if (key == 's') {

void drawLines() {
    // Begin loop for columns
    for (int i = 0; i < cols; i++) {
    // Begin loop for rows
        for (int j = 0; j < rows; j++) {
            int x = i*videoScale;
            int y = j*videoScale;

I have looked into the relevant documentation on PDF exports but could not find a solution to this. Any help would be greatly appreciated!

i run your code as it is gives this

This is the png

if i understand your issue i think may it is with your pdf software your sketch gives pdf and png same

Thank you for your answer.
This is so weird! Any advice on how to go about fixing this?

i run it with google chrome pdf

I think the problem occurs when I export the sketch to pdf. Once the pdf file is created, it is already corrupted to opening with Chrome doesn’t fix the issue. I am not sure how to go about doing this…

i have exported to application also works fine

Try removing pixelDensity(2). If you must use it, then you’ll need to write a second drawLines() using different x,y values for the lines in the pdf (videoScale cut in half). See StackOverflow answer:

1 Like

True! The answer on StackOverFlow was the solution to my problem. It all had to do with the fact that I was using pixelDensity(2).

1 Like

good that it is solved

The question remains, why does the problem appear in one operating system and not appear in other operating systems?

@jafal - I’m curious about the displayDensity() of your system. Is there any way that you could check it and report back?


Adding to topic:

I could not test pixelDensity(2) as it defaulted to pixelDensity = 1.
You could also use text() to write to the sketch window if you do not have a console.


@glv - I surmise that’s the answer to @jafal’s observation which is why I asked him to check it. Haven’t heard back yet.

@svan hi

so what i understand if pixelDensity(2) is not available for the display sketch run normal ??? even included pixelDensity(2) is that true or what ?

so what i understand if pixelDensity(2) is not available for the display sketch run normal ??? even included pixelDensity(2) is that true or what ?

Correct. Based on your report of a displayDensity of 1, and @glv’s findings I think it defaults to normal if a high density display (displayDensity == 2) is not available (even if it’s coded for pixelDensity(2)).

1 Like

now it is clear and explained