0

I'm making a utility to grammatically making a document and printing it.

I am gonna apologize, I'm not good at formulating this and there may be some confusion, please bare over with me :(

This project is mostly for making a user friendly utility for creating documents in your program and printing it and also as a learning experience.

It uses objects which I will convert to MarkDown as its easy to work with, I convert it to HTML as its going to be used in a web form.

The problem occurs when I want the program to print this HTML file that i have now created to a PDF.

I've searched for a solution for a very long time but have yet to find results, all that I've found was either out of date, filled with bugs or just could not do what I wanted.

I have tried iText as that seems to be the most popular choice but as stated above, everything I found was out of date and no longer useful, mostly because the classes they speak of do no longer exist.

What I do want is eventually independence of other libraries so that is my ultimate goal, but for now I want a way to print a locally stored HTML file as a PDF, it needs to be CSS compatible.

As an extra thing if anyone could point me in the direction of learning how to make MarkDown to HTML and printing HTML to PDF I would be more than happy.

Edit 1: Stephan C pointet the question is too wide and difficult to answer, I will try to explain:

Main thing I want is to print a PDF file using an HTML file and CSS which is stored locally.

Optional solutions is I would love to learn how to code this myself instead of implementing libraries, for example markdown4j and iText. Yes I know that's a lot to take on, but as stated this is a learning experience for me.

Simon Jensen
  • 488
  • 1
  • 3
  • 19
  • i would still go with the iText solution. It has all the functionality you need. Dont know which part of it you found difficult to implement.... – MaVRoSCy May 13 '15 at 13:02
  • 3
    OK here is a serious comment. You question is too broad and too unclear / contradictory. You are variously asking how to "print" PDF, convert HTML to PDF, "store" HTML as PDF (which makes no sense). Then you say that you want "independence" for libraries (which makes no sense). Then you ask about converting Markdown to HTML. There are too many contradictory subquestions here for a proper answer. If you want people to take your question seriously, you will need to address these issues. – Stephen C May 13 '15 at 13:03
  • Hi Simon, did you tried all the libraries described here: http://stackoverflow.com/questions/6118635/what-is-the-best-pdf-open-source-library-for-java ? What didn't worked with them? The Flying Saucer project seems to do XHTML to PDF output. – Etienne C May 13 '15 at 13:03
  • Ok long story short, I want to print a locally stored HTML document to PDF with CSS compatibility, rest is optional, thank you for pointing that out, I will try reformulate my question. – Simon Jensen May 13 '15 at 13:05

2 Answers2

0

Printing directly from HTML to pdf can be also worse than using iText or other libraries.
I just made some iText PDF not using low level pdf classes but compiling a PDF form from it.
If you need to fill a PDF form, you can use iText as following.

  1. Make your html form a PDF form. (acrobat or other free softwares)
  2. Use iText classes AcroFields to get the PDF form fields (you get them by names or you can return a map of fields using AcroFields.getFields();
  3. Compile the required informations using AcroFields.setFields() or .setField();
  4. PdfReader class allow you to open an existing PDF (the template)
  5. PdfStamper allow you to write the stream to another one (the result)
    Adding images and other binary stuff is really easy!

By this way, you can do it quickly and layouts will be fast and good too. The limit is that you need as many template as many data you need. (if you have fields that is some cases are many in other just one, you need twice templates). For a static number of fields is a good solution. Hope it helps.

hitz
  • 1,100
  • 8
  • 12
Yuri Blanc
  • 636
  • 10
  • 24
0

Have a look at Converting HTML files to PDF. It has sample code for using with iText and also other suggestions. If you run into problems with approach suggested post specifics here. Either modify your original question or ask new question.

Community
  • 1
  • 1
hitz
  • 1,100
  • 8
  • 12