pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: Issues with Requested page with index 1728 was not written before.
Date Thu, 08 Jun 2017 14:52:23 GMT
Hi,

Difficult to tell because it isn't the full code.
After closing targetDoc you didn't assign null to it, so it will be 
reused here:

             // instantiate the target pdf file, if it has not been 
instantiated
             if (targetDoc == null) {
                 targetDoc = new PDDocument();
             }

Similar problem with font. You cannot use the same PDFont with several 
documents.

If this doesn't help, please reduce your code to a Short, Self 
Contained, Correct, Example:
http://sscce.org/
https://stackoverflow.com/help/mcve .

Tilman


Am 07.06.2017 um 22:18 schrieb Leon Tian:
> Hi
>
> I met below exception when trying to generate PDF files. I use the BatangChe TrueTypeFont
to generate PDF for Korean language. I have a batch of 100 files to process and it usually
starts to giving this error towards the end.
>
> [err] java.io.IOException: Requested page with index 1728 was not written before.
> [err]     at org.apache.pdfbox.io.ScratchFile.readPage(ScratchFile.java:303)
> [err]     at org.apache.pdfbox.io.ScratchFileBuffer.read(ScratchFileBuffer.java:460)
> [err]     at java.io.InputStream.read(Unknown Source)
> [err]     at org.apache.pdfbox.filter.FlateFilter.decompress(FlateFilter.java:133)
> [err]     at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:73)
> [err]     at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:69)
> [err]     at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:162)
> [err]     at org.apache.pdfbox.pdmodel.common.PDStream.createInputStream(PDStream.java:235)
> [err]     at org.apache.pdfbox.pdmodel.common.PDStream.toByteArray(PDStream.java:499)
> [err]     at org.apache.pdfbox.pdmodel.font.TrueTypeEmbedder.buildFontFile2(TrueTypeEmbedder.java:104)
> [err]     at org.apache.pdfbox.pdmodel.font.TrueTypeEmbedder.subset(TrueTypeEmbedder.java:331)
> [err]     at org.apache.pdfbox.pdmodel.font.PDType0Font.subset(PDType0Font.java:162)
> [err]     at com.ibm.esh.bluemix.iol.pdf.PDFCreator.createPDF(IOLPDFCreator.java:227)
>
>
> Here's the code that I use
>
>      public byte[] createPDF(String[] text) throws IOLPDFException{
>          byte[] pdfBytes = null;
>          PDDocument sourceDoc = null;
>
>          try {
>              // get the source PDF template page
>              sourceDoc = PDDocument.load(IOLPDFCreator.class.getResourceAsStream(sourcePath));
>              PDPage templatePage = sourceDoc.getPage(0);
>
>              // instantiate the target pdf file, if it has not been instantiated
>              if (targetDoc == null) {
>                  targetDoc = new PDDocument();
>              }
>
>              if (font == null) {
>                  loadFont(targetDoc);
>              }
>
>              PDPageContentStream contents = null;
>
>              int pageNum = 0;
>              int rowNum = 0;
>
>              for (int i=0; i< text.length; i++) {
>
>                  // Create a Pattern object based on regex for top page line
>                  Pattern rLine = Pattern.compile("PAGE : 0\\d\\d\\d");
>                  // Create matcher object.
>                  Matcher mLine = rLine.matcher(text[i]);
>
>                  //if page top line is found, initialize a new page
>                  if (mLine.find())  {
>                      //if not the first page, close the previous page content stream
>                      if (contents!=null) {
>                          contents.close();
>                      }
>                      //get a blank template page imported
>                      targetDoc.importPage(templatePage);
>                      PDPage page = targetDoc.getPage(pageNum);
>                      contents = new PDPageContentStream(targetDoc, page, AppendMode.APPEND,
true, true);
>                      rowNum = 0;
>                      pageNum ++;
>                      continue;
>                  }
>
>                  // write the text line onto the page
>                  contents.beginText();
>                  contents.setFont(font, size);
>                  contents.newLineAtOffset(x, y - rowNum*space);
>                  contents.showText(text[i]);
>                  contents.endText();
>                  rowNum++;
>              }
>
>              //close the last page content stream
>              contents.close();
>
>              //save the document and get the bytes
>              ByteArrayOutputStream out = new ByteArrayOutputStream();
>              targetDoc.save(out);
>              pdfBytes = out.toByteArray();
>
>              return pdfBytes;
>
>          } catch (IOException ie){
>              System.out.println("IOException while creating pdf...");
>              ie.printStackTrace();
>              throw new PDFException("IOL createPDF encountered IOexception...");
>          } catch (Exception ex) {
>              ex.printStackTrace();
>              throw new PDFException("IOL createPDF encountered exception...");
>          } finally {
>              //close both targetDoc and sourceDoc
>              try {
>                  if (targetDoc != null) {
>                      targetDoc.close();
>                  }
>                  if (sourceDoc != null) {
>                      sourceDoc.close();
>                  }
>              } catch (IOException ie){
>                  System.out.println("IOException while closing document...");
>                  ie.printStackTrace();
>                  throw new PDFException("IOL createPDF encountered IOexception...");
>              }
>          }
>
>      }
>
>
> Your help is appreciated!
>
> <http://aka.ms/weboutlook>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message