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?

10
  • 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 8, 2019 at 15:46
  • Other options include: ship Pandoc 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 8, 2019 at 15:53
  • By the way, I think this question is more suitable for Software recommendations
    – Magnetron
    Mar 8, 2019 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 8, 2019 at 16:39

2 Answers 2

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.

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. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.