I am trying to create a PDF with image content that has a transparent background. I have tried various methods with disappointing results.
I tried the direct-to-pdf
beginRecord(PDF, "filename.pdf") method:
- import processing.pdf.*;
- void setup() {
- size(580, 580);
- noLoop();
- beginRecord(PDF, "image_test_direct_pdf.pdf");
- }
- void draw() {
- PImage img;
- img = loadImage("moon.0096_cr.png");
- imageMode(CENTER);
- background(0);
- pushMatrix();
- translate(width/2, height/2);
- image(img,0,0,width/4, height/4);
- popMatrix();
- endRecord();
- }
This looks great and has 180ppi resolution but the transparency has been replaced with opaque gray.
The buffered pdf surface
(PGraphicsPDF) createGraphics(width, height, PDF, "filename.pdf") method:
- import processing.pdf.*;
- PGraphicsPDF pg;
- void setup() {
- size(580, 580);
- pg = (PGraphicsPDF) createGraphics(width, height, PDF, "image_test_pdf_createGraphics.pdf");
- noLoop(); // Stops Processing from continuously executing the code within draw()
- }
- void draw() {
- beginRecord(pg);
- PImage img;
- img = loadImage("moon.0096_cr.png");
- imageMode(CENTER);
- image(img, width/2, height/2);
- pg.imageMode(CENTER);
- pg.background(0);
- pg.pushMatrix();
- pg.translate(width/2, height/2);
- pg.image(img,0,0,width/4, height/4);
- pg.popMatrix();
- endRecord();
- }
Interestingly enough, this also looks just as good but Acrobat is showing only 72ppi. What's up with that?! Same situation with the transparency.
The off-screen buffer
createGraphics(width, height) method:
- import processing.pdf.*;
- PGraphics pg;
- void setup() {
- size(580, 580);
- pg = createGraphics(width, height);
- noLoop(); // Stops Processing from continuously executing the code within draw()
- }
- void draw() {
- pg.beginDraw();
- PImage img;
- img = loadImage("moon.0096_cr.png");
- pg.imageMode(CENTER);
- pg.background(0);
- pushMatrix();
- pg.translate(width/2, height/2);
- pg.image(img,0,0,width/4, height/4);
- popMatrix();
- pg.endDraw();
- image(pg,0,0);
- pg.save("image_test_createGraphics.png");
- beginRecord(PDF, "image_test_createGraphics.pdf");
- image(pg, 0, 0);
- endRecord();
- }
Now transparency is being preserved but the resolution is unacceptable. Again it is showing as 72ppi and this time it looks it...
On the PDF Export library page it says
Images don't look great, mostly because of the difference of expectations in how a PDF should look (scalable and high res) versus what happens when image data is written to it at 72 dpi.
And yet in the first example above, image data was written at 180ppi. I don't know why 72ppi would be the default. It is ridiculously low even for a small-screen monitor. But for a PDF which will likely be printed, 300-1200 ppi is the norm. Why isn't this adjustable? Is there a hack that can make it adjustable?
The PDF version of the exported files is 1.4 (Acrobat 5.x), which supports an alpha channel. I even made a PDF of the same version with the original image and the background was transparent. This also should be programmable. Is there a hack for this?
So I don't know where to go from here. It's either horrible resolution or no transparencies. I can't proceed with either situation. Does anyone know a work-around that will give me high resolution AND transparency at the same time. I still need scalable vector graphics as well for text and graphics objects so a high-res image is not an option.