【问题标题】:Issues exporting Processing sketch to PDF: sketch gets cropped将处理草图导出为 PDF 的问题:草图被裁剪
【发布时间】:2021-12-28 20:38:53
【问题描述】:

我正在尝试将我的草图导出为 pdf。我遇到的问题是,由于某种原因,我的草图只有一部分被导出为 pdf,就好像原始草图被裁剪了一样!当我运行我的草图时,会显示 64 条线(如预期的那样),实际上,当我将它保存为 png 时,所有 64 条线都在那里,并且草图看起来与我运行它时一样。

不过,当我将草图导出为 pdf 时,只显示 16 行,好像 pdf 正在裁剪我的草图,并且只有裁剪的部分正在被裁剪已导出。

这是显示草图应该是什么样子的 png:

这是 pdf 导出的内容:

这是我的代码:

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);
        }
    }
}

我查看了有关 PDF 导出的相关文档,但找不到解决方案。任何帮助将不胜感激!

【问题讨论】:

    标签: java pdf processing export-to-pdf generative-art


    【解决方案1】:

    从 setup() 中移除 pi​​xelDensity(2) 以进行修复。 PixelDensity 为 2 旨在允许视网膜监视器使用所有像素。如果必须使用它,则需要为 pdf 文件编写单独的 drawLines()(示例如下)。请注意,对于 pdf drawLines(),videoScale 被切成两半,每条线的第二组 x,y 坐标是 +15 而不是 +30。您还必须更改保存的每个文件的路径以适合您的系统。我使用了另一种方法从 pGraphics 创建 pdf,这应该是无关紧要的。

    /*
     If pixelDensity(2) is used, need to modify second drawLines() for pdf.
     Change file paths to suit your system.
    */
    
    import processing.pdf.*;
    import java.util.Random;
    
    int cols, rows;
    int videoScale = 100;
    
    void drawLines() {
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          int x = i*videoScale;
          int y = j*videoScale;
          strokeWeight(1.5);
          line(x, y, x+30, y+30);
        }
      }
    }
    
    void setup() {
      size(800, 800);
      background(255);
      pixelDensity(2);
      // frameRate(0.5);
      cols = width/videoScale;
      rows = height/videoScale;   
      drawLines();
    }
    
    void draw() {
    }
    
    void keyPressed() {
      if (key == 'p') {
        PGraphics pdf = createGraphics(width, height, PDF, "/Users/me/Desktop/output.pdf"); // change this
        videoScale = 50;  // cut in half
        pdf.beginDraw();  
        for (int i = 0; i < cols; i++) {
          for (int j = 0; j < rows; j++) {
            int x = i*videoScale;
            int y = j*videoScale;
            pdf.line(x, y, x+15, y+15); // +15 instead of +30 for second x,y coordinates
          }
        }
        pdf.dispose();
        pdf.endDraw();
        println("Printed pdf.");
      }
      if (key == 's') {
        saveFrame("/Users/me/Desktop/img.png"); // change this
        println("Image saved.");
      }
    }
    
    

    【讨论】:

    • 非常感谢您的回答。它解决了我的问题,也让我了解了问题发生的原因。老实说,非常感谢你,我真的很感激!向你致敬:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多