Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a PNG file to PDF using java?

Tags:

java

pdf

png

Are there any open source libraries that I can use?

like image 891
Saurav Shah Avatar asked Dec 02 '11 19:12

Saurav Shah


People also ask

How do I convert a PNG image to PDF?

It's easy — simply go to Adobe Acrobat online services from any web browser and navigate to the convert JPG to PDF page. Click the Select A File button or drag and drop the image file into the drop zone to upload. You can upload a variety of image types to convert to a PDF, including a: PNG.

Can you convert PNG to PDF for free?

PDF. online provides a free online file converter for you to change your PNG images to PDF files. Simply select the PNG you want to convert, and use the online converter to turn your PNG file into a PDF.


1 Answers

An example which rotates the page, if landscape mode fits better

/**
 * Converts arbitrary image file to PDF
 * http://stackoverflow.com/a/42937466/241986
 * @param imageFile contents of JPEG or PNG file
 * @param outputStream stream to write out pdf, always closed after this method execution.
 * @throws IOException when there' an actual exception or image is not valid
 */
public static void imageToPdf(byte[] imageFile, OutputStream outputStream) throws IOException {
    try {
        Image image;
        try {
            image = Image.getInstance(imageFile);
        } catch (BadElementException bee) {
            throw new IOException(bee);
        }

        //See http://stackoverflow.com/questions/1373035/how-do-i-scale-one-rectangle-to-the-maximum-size-possible-within-another-rectang
        Rectangle A4 = PageSize.A4;

        float scalePortrait = Math.min(A4.getWidth() / image.getWidth(),
                A4.getHeight() / image.getHeight());

        float scaleLandscape = Math.min(A4.getHeight() / image.getWidth(),
                A4.getWidth() / image.getHeight());

        // We try to occupy as much space as possible
        // Sportrait = (w*scalePortrait) * (h*scalePortrait)
        // Slandscape = (w*scaleLandscape) * (h*scaleLandscape)

        // therefore the bigger area is where we have bigger scale
        boolean isLandscape = scaleLandscape > scalePortrait;

        float w;
        float h;
        if (isLandscape) {
            A4 = A4.rotate();
            w = image.getWidth() * scaleLandscape;
            h = image.getHeight() * scaleLandscape;
        } else {
            w = image.getWidth() * scalePortrait;
            h = image.getHeight() * scalePortrait;
        }

        Document document = new Document(A4, 10, 10, 10, 10);

        try {
            PdfWriter.getInstance(document, outputStream);
        } catch (DocumentException e) {
            throw new IOException(e);
        }
        document.open();
        try {
            image.scaleAbsolute(w, h);
            float posH = (A4.getHeight() - h) / 2;
            float posW = (A4.getWidth() - w) / 2;

            image.setAbsolutePosition(posW, posH);
            image.setBorder(Image.NO_BORDER);
            image.setBorderWidth(0);

            try {
                document.newPage();
                document.add(image);
            } catch (DocumentException de) {
                throw new IOException(de);
            }
        } finally {
            document.close();
        }
    } finally {
        outputStream.close();
    }
}

Inside pom.xml, one of free iText forks, if you are not already using iText

<dependency>
    <groupId>com.github.librepdf</groupId>
    <artifactId>openpdf</artifactId>
    <version>1.0.1</version>
</dependency>
like image 82
Boris Treukhov Avatar answered Oct 21 '22 10:10

Boris Treukhov