[ 
https://issues.apache.org/jira/browse/PDFBOX-5967?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17932556#comment-17932556
 ] 

Tilman Hausherr edited comment on PDFBOX-5967 at 3/5/25 9:07 AM:
-----------------------------------------------------------------

Here's some code:
{code:java}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class PDFBox5967 implements Printable
{
    public static void main(String[] args) throws IOException, PrinterException
    {
        new PDFBox5967().doStuff();
    }

    void doStuff() throws IOException, PrinterException
    {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        if (job.printDialog())
        {
            job.print();
        }
    }

    public void renderStuff(Graphics2D g, PageFormat pageFormat)
    {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, 
RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
RenderingHints.VALUE_INTERPOLATION_BICUBIC);

        BufferedImage originalImage;
        File imageFile = new File("PDFBOX-5967-reduced1.png");
        try
        {
            originalImage = ImageIO.read(imageFile);
        }
        catch (IOException ex)
        {
            throw new RuntimeException(ex);
        }

        // copy to bw image
        BufferedImage bwImage = new BufferedImage(originalImage.getWidth(), 
originalImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
        Graphics2D bwGraphics = (Graphics2D) bwImage.getGraphics();
        bwGraphics.drawImage(originalImage, 0, 0, null);
        bwGraphics.dispose();

        AffineTransform at = new AffineTransform();
        at.translate(0, pageFormat.getHeight());
        at.scale(1, -1);
        at.scale(0.5, 0.5);
        at.translate(0, 400);
        g.transform(at);
        g.drawImage(bwImage, 50, 50, null);
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 
throws PrinterException
    {
        if (pageIndex != 0)
        {
            return NO_SUCH_PAGE;
        }
        renderStuff((Graphics2D) graphics, pageFormat);
        return PAGE_EXISTS;
    }
}
{code}
You'd need to adjust this somewhat 1) adjust scale / position so that it 
appears on your tiny label 2) play with the rendering hints if the effect 
doesn't occur. Btw our print utility has an option for barcodes 
("-noColorOpt"), maybe try that one if you didn't already.


was (Author: tilman):
Here's some code:
{code:java}
package jdk9test;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class PDFBox5967 implements Printable
{
    public static void main(String[] args) throws IOException, PrinterException
    {
        new PDFBox5967().doStuff();
    }

    void doStuff() throws IOException, PrinterException
    {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        if (job.printDialog())
        {
            job.print();
        }
    }

    public void renderStuff(Graphics2D g, PageFormat pageFormat)
    {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, 
RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
RenderingHints.VALUE_INTERPOLATION_BICUBIC);

        BufferedImage originalImage;
        File imageFile = new File("PDFBOX-5967-reduced1.png");
        try
        {
            originalImage = ImageIO.read(imageFile);
        }
        catch (IOException ex)
        {
            throw new RuntimeException(ex);
        }

        // copy to bw image
        BufferedImage bwImage = new BufferedImage(originalImage.getWidth(), 
originalImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
        Graphics2D bwGraphics = (Graphics2D) bwImage.getGraphics();
        bwGraphics.drawImage(originalImage, 0, 0, null);
        bwGraphics.dispose();

        AffineTransform at = new AffineTransform();
        at.translate(0, pageFormat.getHeight());
        at.scale(1, -1);
        at.scale(0.5, 0.5);
        at.translate(0, 400);
        g.transform(at);
        g.drawImage(bwImage, 50, 50, null);
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 
throws PrinterException
    {
        if (pageIndex != 0)
        {
            return NO_SUCH_PAGE;
        }
        renderStuff((Graphics2D) graphics, pageFormat);
        return PAGE_EXISTS;
    }
}
{code}
You'd need to adjust this somewhat 1) adjust scale / position so that it 
appears on your tiny label 2) play with the rendering hints if the effect 
doesn't occur. Btw our print utility has an option for barcodes 
("-noColorOpt"), maybe try that one if you didn't already.

> Printed barcodes are fuzzy on macOS only
> ----------------------------------------
>
>                 Key: PDFBOX-5967
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5967
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.33, 3.0.4 PDFBox
>         Environment: macOS 15.3.1 (Intel and Apple Silicon)
> openjdk 11.0.25, openjdk 23.0.1
>            Reporter: Tres Finocchiaro
>            Priority: Major
>              Labels: macOS, print
>         Attachments: PDFBOX-5967-reduced1.pdf, PDFBOX-5967-reduced1.png, 
> pb_usps_sample.pdf, print_comparison.png
>
>
> The attached PDF prints well on Windows and Linux, but on macOS the barcodes 
> are fuzzy and unscannable. We print a lot of 4x6 labels using pdfbox and 
> CUPS, so I'm not sure what is different about this label. It's created by 
> Pitney Bowes through their shipping API.
>  * I understand that this may be a result of something happening at the 
> OS-level.  If this is the case, any guidance as to how to isolate and provide 
> a bug report for openjdk is greatly appreciated.
>  * If there's a workaround that can be used as a stop-gap leveraging the 
> PDFBOX API, that would also be greatly appreciated.
> *Steps to reproduce:*
>  
>  * *PDFBOX 3.0*
> {{java -jar ~/Downloads/pdfbox-app-2.0.33.jar PrintPDF 
> ~/Downloads/pb_usps_sample.pdf -mediaSize=4x6}}
>  * *PDFBOX 2.0*
> {{java -jar ~/Downloads/pdfbox-app-2.0.33.jar PrintPDF 
> ~/Downloads/pb_usps_sample.pdf}}
>  Tests were performed on physical hardware (Zebra ZD620 using the CUPS ZPL 
> driver) with openjdk 11.0.25 and openjdk 23.0.1.
> [^pb_usps_sample.pdf]
>  
> !print_comparison.png!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org

Reply via email to