3

I had this stupid idea of creating a template as a .docx or .rtf or .pdf and then replacing the text in that document to generate reports. This seemed like a better way of doing it than using paid reporting software.

Well, I believe I've tried just about everything now and I'm amazed at how impossible it is to do anything with pdfs.

Try 1

HTML -> PDF

A lot harder to design the template. It doesn't look the same when you print it. Never got it working outside of a command line example (not sure how well, say, iTextSharp-LGPL would even work or if it could handle base64 strings as I'm not sure how else you are going to tell it about images). In any case, doing it this way makes it too hard to design the template.

Try 2

OpenXml -> PDF

I stupidly assumed that because Word could save as PDF that OpenXml could to. I was wrong. It cannot save as a PDF.

Try 3

OpenOffice/LibreOffice (docX -> PDF)

It can't read OpenXml which is a problem because I was editing the template as OpenXml and then saving that result (as a .docx) but it can't read that saved document.

Try 4

iTextSharp LGPL

This one just doesn't work, lol. And apparently even though when you google "convert rtf to pdf" the ONLY thing that comes up is iText and its derivatives it doesn't convert rtf documents to pdf documents. I verified this myself (it only saves the text not the formatting) and later found this post to convince me I wasn't doing something wrong.

Try 5

PDF -> PDF

Since converting ANYTHING to a PDF seems to be impossible maybe I can save the template as a PDF and just do a text replace on that. Nope, lol, that is apparently a very difficult thing to do.

Try 6

Pandoc (.odt/.docx -> pdf), (.rtf -> .pdf not supported)

pandoc mockup2.odt -s -o mockup2.pdf

link to the files in the picture. *note, it messes up in the same way if you try converting .odt/.docx to .tex. enter image description here

What do I do here? Buy software so that I can save a file as PDF? Is that the only option?

halfer
  • 19,824
  • 17
  • 99
  • 186
user875234
  • 2,399
  • 7
  • 31
  • 49
  • Well, if you have MS Word installed in the target machine you could use the Word COM interface in C# to convert any document that Word can open to pdf. – Magnetron Mar 08 '19 at 15:46
  • [That's frowned upon in a server environment, unfortunately](https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office). – user875234 Mar 08 '19 at 15:53
  • Other options include: ship [Pandoc](https://pandoc.org/index.html) with your program and call direct in the comand line via C# code to convert many input formats to pdf; Use a pdf library to generate pdfs by hand; Create a template in LaTeX and ship LaTeX with your software and convert it to pdf via cmd call in C# code; – Magnetron Mar 08 '19 at 15:53
  • By the way, I think this question is more suitable for [Software recommendations](https://softwarerecs.stackexchange.com/) – Magnetron Mar 08 '19 at 16:00
  • Pandoc looks promising but it's not generating anything correctly with the documents I'm trying to use. I'm still experimenting with it but so far it's generating invalid output. – user875234 Mar 08 '19 at 16:39
  • Can you share the document you're trying to convert and the command you're using? Looks like [you need latex installed to output pdf](https://pandoc.org/MANUAL.html#creating-a-pdf) – Magnetron Mar 08 '19 at 16:41
  • I updated my post with a picture of the pandoc results and also a link to the input and output file from the picture. – user875234 Mar 08 '19 at 17:33
  • Yeah, it's a little hard to achieve the same look and feel, specially with tables. That comes down to the other two solutions I wrote before, either create the pdf by hand with iTextSharp or oyher libraries, or learn LaTeX (it's a txt with special commands, you could use special editors to help you create the template, like [LyX](https://www.lyx.org/)), create a template anyway you like, fill by code with your data and call pdftext in cmd using c# code. – Magnetron Mar 08 '19 at 17:48
  • @Magnetron My bad. I placed the comment at wrong place. I've deleted it. You can delete your comment, as well if you like so as to avoid clutter. I'll do the same for this comment, as well. Thanks. – nam Mar 08 '19 at 19:52
  • This is probably not an on-topic question, but did you evaluate JasperReports? It run on Java, but provides an API which C# could use. – halfer May 17 '19 at 19:38

2 Answers2

2

I have a solution. I'm not saying it's the best solution. LibreOffice (or possibly OpenOffice if you are so inclined) accepts command line arguments that will do the switch.

soffice.exe --headless --convert-to pdf mockup.odt

*note - this is after I added libreoffice to my path (C:\Program Files\LibreOffice\program). idk why it's called soffice.exe instead of libreoffice.exe.

user875234
  • 2,399
  • 7
  • 31
  • 49
0

I might have a working solution for you, if you are stuck with the docx-file for the template. I found one free solution for docx to pdf conversions, without using microsoft.interop, etc.: See first answer in this stack overflow post

It uses two tools: The open xml power tools and DinkToPdf (Which is essentially a wkhtmltopdf wrapper). The html to pdf part works just fine, but the docx to html part looks like a catastrophe at first. You can fix this with custom css (There are some resources online).

Powertools-.NetStandard

DinkToPdf-GitHub

There are more possibilities for proprietary software, like Asposes.Words and Syncfusion file-formats. Most of the proprietary solutions are pretty expensive...

If you are just working on a Windows Environment, where MS-Office is installed, you can use Microsoft.Interop. It is by far the easiest solution (In this post, Interop is mentioned several times Stackoverflow Word to PDF

If you found another (better) working solution, please let me know. I still have not decided if I will use a proprietary or a free solution. :-)

Alex B
  • 15
  • 7