Itext PDF shows blank page

2019-09-18 13:19发布

问题:

I have the below sample code which generates a PDF file using iText.

The question I have is when I create the base64Binary by DatatypeConverter.printBase64Binary method.. I tried to copy the Sysem.out.println of "base64Binary". Used a online base64 online decoder tool to decode the content and save it output as sample.pdf and when I try to open the sample.pdf it shows empty. I am not sure why its behaving this way and help would be much appreciated. But when I directly decode using java and write it to a disk file it shows the context.

Can someone help me understand why it shows blank when I try to save "base64Binary" output as sample.pdf.

Thanks.

Below is the code snippet:

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.DatatypeConverter;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Creates a PDF file in memory.
 */
public class HelloWorldMemory {

    /** Path to the resulting PDF file. */
    public static final String RESULT = "C:////hello_memory.pdf";

    public static void main(final String[] args) throws DocumentException, IOException {
        // step 1
        final Document document = new Document();

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PdfWriter writer = PdfWriter.getInstance(document, baos);

        document.open();
        final PdfContentByte cb = writer.getDirectContent();

        cb.beginText();
        cb.setFontAndSize(getBaseFont(Font.NORMAL), 24);
        final float exPosition = (PageSize.A4.getWidth()) / 2;
        cb.showTextAligned(Element.ALIGN_CENTER, "Test No", exPosition, 670, 0);
        cb.endText();
        document.add(new Paragraph("Hello World!"));

        document.close();
        System.out.println("baos.toByteArray():" + baos.toByteArray());
        final String base64Binary = DatatypeConverter.printBase64Binary(baos.toByteArray());
        System.out.println("base64Binary:" + base64Binary);
        final byte[] txt = DatatypeConverter.parseBase64Binary(base64Binary);

        final FileOutputStream fos = new FileOutputStream(RESULT);
        fos.write(txt);

        fos.close();
    }

    private static BaseFont getBaseFont(final int fontType) {
        final Font f = new Font(FontFamily.HELVETICA, 0, fontType);
        final BaseFont baseFont = f.getCalculatedBaseFont(true);
        return baseFont;
    }
}

回答1:

PDF is a binary file format based on the Carousel Object System (COS) syntax and the AIM (Adobe Imaging Model). The COS objects use ASCII for the file structures, but the streams for images and AIM syntax are usually binary. When you copy a PDF file without respecting the binary aspect of the file, a PDF viewer can render the document structure (the pages) based on the ASCII COS objects, but not the content (what's on the pages). This is probably what happens in your case: you're corrupting the bytes in the content streams.



回答2:

This question is not really related to iText or PDF. You'll have the same problem with any binary data that is base64 encoded. When using the online base64 decoder, your binary data gets corrupted somehow. Bruno already explained in his answer why this does not completely invalidate a file in case of a PDF.

The data is probably corrupted because of encoding issues. Maybe the online base64 decoder displayed the decoded data in a textarea or something and you copy/pasted it into a file? If you use a decoder that offers you a binary file for download, the result should be fine.

I tested with http://www.opinionatedgeek.com/dotnet/tools/base64decode/ (the first hit of a Google search). When I save the .bin file and rename it to .pdf, it displays as expected in a PDF viewer.