java 手机页面pdf显示不出来,Itext PDF显示空白页面

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;

}

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值