• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Printing plain text files to PDF printer using javax.print results in an empty file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to create a pdf file from plain text files. I supposed that the simplest method would be read these files and print them to a PDF printer. My problem is that if I print to a pdf printer, the result will be an empty pdf file. If I print to Microsoft XPS Document Writer, the file is created in plain text format, not in oxps format. I would be satisfied with a two or three step solution. (Eg. converting to xps first then to pdf using ghostscript, or something similar). I have tried a couple of pdf printers such as: CutePDF, Microsoft PDF writer, Bullzip PDF. The result is the same for each one.

The environment is Java 1.7/1.8 Win10



TIA, Sandor
 
Greenhorn
Posts: 23
1
IBM DB2 Netbeans IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandor,

I have used Apache PDFBox successfully creating and editing PDF files.
https://pdfbox.apache.org/


Below is a quick hack where I demonstrate how to read a text file and create a pdf file of that text file.

 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Sandor Feher
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the advice. After I had partially success with a cups pdf printer I ended up to pdfbox finally. Now my text looks like I expected .
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know it's a bit late to provide a solution in such an old post, but I hope that my answer can help someone who finds this post by searching.

This scenario is based on Spire.PDF for Java, which a class library for PDF document. Before running the following code snippet to create PDF from text files, you'll need to add a dependency to Spire.Pdf.jar in your program.

 
reply
    Bookmark Topic Watch Topic
  • New Topic