# Issues exporting sketch to PDF: sketch gets cropped!

**URL:** <https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344>\
**Category:** Coding Questions\
**Created:** [December 28, 2021, 8:33pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344 "2021-12-28T20:33:58Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![tommsmoro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tommsmoro/32/15296_2.png) [@tommsmoro](https://discourse.processing.org/u/tommsmoro)\
**Post date:** [December 28, 2021, 8:33pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/1 "2021-12-28T20:33:58Z")

</div>

**UPDATE: PROBLEM SOLVED**  
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](https://stackoverflow.com/questions/70512258/issues-exporting-processing-sketch-to-pdf-sketch-gets-cropped/70513179#70513179)

########  
**ORIGINAL POST BELOW**  
#######

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** :

 ![img](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/42f1cb3a6de47636fab41f6f49fca7a5f7f5b88d.png)

**This is what the pdf has exported**

 ![Screenshot 2021-12-28 at 21.32.11](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/255294e7dea61e6a57576d0b909cb2762dc135eb.png)

And this is my code:

```auto

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

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

void setup() {
  size(800,800);
  pixelDensity(2);
  frameRate(0.5);

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

}

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

  background(255);
  strokeWeight(1.5);
  drawLines(); 

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

void keyPressed() {
    if (key == 'p') {
        recordPDF = true;
    }
    if (key == 's') {
        saveFrame("img.png");
    }
}

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;
            line(x,y,x+30,y+30);
        }
    }
}

```

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

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 28, 2021, 8:59pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/2 "2021-12-28T20:59:45Z")

</div>

hi  
i run your code as it is gives this

 ![Untitled 223](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8b3add1446bff242b88cb3625d148d7568cf5221.jpeg)

**This is the png**

 ![img](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1d54ec0b5c36acdccd55db0e5a88fbb7a29d3b27.png)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 28, 2021, 9:10pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/3 "2021-12-28T21:10:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tommsmoro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tommsmoro/32/15296_2.png) [@tommsmoro](https://discourse.processing.org/u/tommsmoro)\
**Post date:** [December 28, 2021, 9:13pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/4 "2021-12-28T21:13:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 28, 2021, 9:19pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/5 "2021-12-28T21:19:09Z")

</div>

i run it with google chrome pdf

---

<div class="post-metadata">

**Author:** ![tommsmoro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tommsmoro/32/15296_2.png) [@tommsmoro](https://discourse.processing.org/u/tommsmoro)\
**Post date:** [December 28, 2021, 9:23pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/6 "2021-12-28T21:23:25Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 28, 2021, 9:36pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/7 "2021-12-28T21:36:39Z")

</div>

i have exported to application also works fine

 ![Untitled 224](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4daa9d0e7d5a94f1f3eaf47e9cd77252c9adf1ec.jpeg)

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [December 28, 2021, 10:26pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/8 "2021-12-28T22:26:34Z")

</div>

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: [https://stackoverflow.com/questions/70512258/issues-exporting-processing-sketch-to-pdf-sketch-gets-cropped](https://stackoverflow.com/questions/70512258/issues-exporting-processing-sketch-to-pdf-sketch-gets-cropped)

---

<div class="post-metadata">

**Author:** ![tommsmoro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tommsmoro/32/15296_2.png) [@tommsmoro](https://discourse.processing.org/u/tommsmoro)\
**Post date:** [December 29, 2021, 9:32am UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/9 "2021-12-29T09:32:22Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 29, 2021, 10:45am UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/10 "2021-12-29T10:45:12Z")

</div>

good that it is solved

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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [December 29, 2021, 3:09pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/11 "2021-12-29T15:09:48Z")

</div>

@jafal - I’m curious about the displayDensity() of your system. Is there any way that you could check it and report back? [https://processing.org/reference/displayDensity\_.html](https://processing.org/reference/displayDensity_.html)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 29, 2021, 4:35pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/12 "2021-12-29T16:35:06Z")

</div>

Hello,

Adding to topic:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f1f12462b4b7929e4d2c74549b9547f24d3ed2f4.png)

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.

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [December 29, 2021, 4:59pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/13 "2021-12-29T16:59:31Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 29, 2021, 5:17pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/14 "2021-12-29T17:17:39Z")

</div>

@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 ?

 ![Untitled 226](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f7d9ea5705e9cd794cbe8601723b8f67e06ed8e1.jpeg)

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [December 29, 2021, 5:43pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/15 "2021-12-29T17:43:51Z")

</div>

> 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)).

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 29, 2021, 5:54pm UTC](https://discourse.processing.org/t/issues-exporting-sketch-to-pdf-sketch-gets-cropped/34344/16 "2021-12-29T17:54:40Z")

</div>

> [@svan](#):
>
> 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)).

now it is clear and explained
