The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

raimanoj.vns (1) [Avatar] Offline
#1
Hi,

I am getting error message while opening a PDF/A compliant pdf file on Windows macjine. This pdf file was generated through iText on Unix machine. The code used to generate this pdf file is:

---------------------------------------------------------------------------------------------------------------
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fmr.xtrac.logging.RUNTIMETrace;
import com.fmr.xtrac.util.XtracUtil;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
* This is a class to generate a PDF report
*/
public class PdfMaker
{
// the _document is the PDF file's layout
private Document _document;

// the writer used to write to the _document to produce the PDF file
private PdfWriter _writer;

// the output stream to write to the file
private ByteArrayOutputStream _baosPDF;

// empty space between two tables, headings, etc
private Paragraph _emptyPara;

// Common constants
private static final String REPORT_HEADING = "REPORT";
private static final String FILE_EXTENSION = ".pdf";
private static final String TIME_PATTERN = "mmddyyyy_HHmmss";
private static final String FOOTER = "footer";

private static final String ITEM1 = "Item1";
private static final String ITEM2 = "Item2";
private static final String ITEM3 = "Item3";


/**
* This method initializes the components required for generating the PDF
*/
private void init()
{
_document = new Document();
_baosPDF = new ByteArrayOutputStream();


try
{
_writer = PdfWriter.getInstance(_document, _baosPDF);
_writer.setPDFXConformance(PdfWriter.PDFA1A); }
catch (DocumentException dex)
{

}
}

/**
* This method creates the PDF
* @return File representation of the PDF generated
*/
public File createPDF()
{

File pdfFile = null;
init();
File fontFile = createFontFile();

try
{
_document.setPageSize(PageSize.A4);
_document.open();
BaseFont bf = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.WINANSI, true);

Font font = new Font(bf, 12);

_emptyPara = new Paragraph(" ", font);

// set the footer
HeaderFooter footer = new HeaderFooter(new Phrase(FOOTER, font), false);
footer.setAlignment(2);
_document.setFooter(footer);

Paragraph title = new Paragraph(REPORT_HEADING, font);
title.setAlignment(1);
_document.add(title);

_document.add(_emptyPara);

// General Details
addParagraphHeader("General", font);
addGeneralDetails(font);
RUNTIMETrace.debug(this, "Added the General details");

_document.close();
StringBuffer pdfFileName = new StringBuffer();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_PATTERN);
String dateAndTimeSuffix = simpleDateFormat.format(new Date());
pdfFileName.append("_");
pdfFileName.append(dateAndTimeSuffix);
pdfFile = File.createTempFile(pdfFileName.toString(), FILE_EXTENSION);
if (RUNTIMETrace.isDebugOn())
{
StringBuffer debugMsg = new StringBuffer(256);
debugMsg.append("PDF file : ");
debugMsg.append(pdfFile.getName());
debugMsg.append("created at location ").append(pdfFile.getCanonicalPath());
RUNTIMETrace.debug(this, debugMsg.toString());
}

_baosPDF.writeTo(new FileOutputStream(pdfFile));
RUNTIMETrace.debug(this, "Done with the PDF");
_writer.close();
}
catch(IOException ioe)
{

}
catch(DocumentException docex)
{

}
finally
{
RUNTIMETrace.debug(this, "Deleting the temporary font file");
XtracUtil.deleteFile(fontFile);
}
return pdfFile;
}

/**
* This method adds a paragraph to the PDF
* @param paragraphName : the name of the Paragraph
*/
private void addParagraphHeader(final String paragraphName, final Font font) throws DocumentException
{
Paragraph generalPara = new Paragraph(paragraphName, font);
generalPara.setAlignment(1);
_document.add(generalPara);
_document.add(_emptyPara);
}

/**
* This method adds the General details of the workitem to the PDF in a tabular form
* @throws DocumentException : an iText specific Runtime Exception
*/
private void addGeneralDetails(final Font font) throws DocumentException
{
String item1 = "Item 1";
String item2 = "Item 2";
String item3 = "Item 3";

_document.add(new Paragraph("Item 1 : " + item1, font));
_document.add(new Paragraph("Item 2 : " + item2, font));
_document.add(new Paragraph("Item 3 : " + item3, font));

}

/**
* Private method for creating font file
* @return File - font File object
*/
private File createFontFile()
{
InputStream fontInputStream =
this.getClass().getResourceAsStream("/config/font/arial.ttf");

OutputStream out = null;
File file = null;
int length;

try
{
String fileNamePrefix = new StringBuffer("TestFont").append(
System.currentTimeMillis()).toString();
file = File.createTempFile(fileNamePrefix, ".ttf");

out = new FileOutputStream(file);
int lengthFromStream = fontInputStream.available();
byte[] buf = new byte[lengthFromStream];
while ((length = fontInputStream.read(buf)) > 0)
{
out.write(buf, 0, length);
}
out.close();
fontInputStream.close();
}
catch (IOException ioe)
{

}
return file;
}

public static void main(String args[])
{
PdfMaker pdfMaker = new PdfMaker();
pdfMaker.createPDF();
}
}

---------------------------------------------------------------------------------------------------------------------

If the file is created on Window machine and opened on Window machine then no error is coming.

Please help me out.

--- Manoj
blowagie (284) [Avatar] Offline
#2
Re: Getting error while opening a PDF/A compliant pdf file
You're not telling us what error you are getting, and you shouldn't use this forum to post questions. Use the mailing list please.