1

I have what seems like a simple task. I wrote a Windows application using C++. Now I need to add to it a capability to print forms -- nothing fancy, just plain text, with lines, tables, and simple graphics. Besides printing, a user needs to be able to preview on the screen all forms being printed.

Previously I was able to get away with this task by using an embedded Internet Explorer control and design all forms in HTML (which I like -- the HTML part.) But the problem comes with IE... hmm... I wish I had a nickel every time I heard that phrase :) Anyway, IE can print an HTML page but it does not provide any easy way for users of my software to customize page size, page margins, etc.

I spent a good deal of the last week trying to make IE Print Templates work with what I need ... but eventually failed. That stuff is very poorly documented and what I was able to do seems to randomly crash on me. So at this point I gave up on IE...

So my question to you -- is there a way to incorporate printing into my C++ program for the purposes like I described above?

Community
  • 1
  • 1
c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 1
    As anything with WinAPI, even merely beginning to think about perhaps starting something already takes a few thousand lines. Not to mention actually doing it. Too broad, IMO. – syam Sep 24 '13 at 21:30
  • Best bet is to get yourself a copy of [Programming Windows](http://www.amazon.com/Programming-Windows%C2%AE-Edition-Microsoft-Series/dp/157231995X) and learn to do it the right way, embedding an IE control clearly is not something sensible to do just to get print functionality. – Crowman Sep 24 '13 at 21:40
  • This question might be a little too broad to get an answer here. Nothing in the Windows API is going to give you a layout and formatting engine as powerful as HTML+CSS. If, on the other hand, you're already drawing these forms in a Window (in a DPI-independent way), then re-using that code to also print is pretty simple. – Adrian McCarthy Sep 24 '13 at 21:54
  • @PaulGriffiths: If "Programming Windows" book explains how to use IE Print Templates with a C++ code samples to handle barely documented IE COM interfaces, I'd buy it in a flash. As for HTML+CSS -- the only issue is printing it. If there are any classes/extensions that can print HTML, I'd be more than happy to use them. – c00000fd Sep 24 '13 at 22:07
  • @c00000fd: Like I said, I'd recommend getting a copy of the book and learning to do it the right way *instead of* doing it a weird way with IE controls. – Crowman Sep 24 '13 at 22:15
  • @PaulGriffiths: Can you share the _right way_ with me? Or is it just a rhetoric? – c00000fd Sep 24 '13 at 22:16
  • @PaulGriffiths: "The right way" is to avoid WinAPI in the first place, because it is non-portable and platform-specific. – SigTerm Sep 24 '13 at 22:24
  • 1
    @SigTerm Pretty hard to print on Windows without something using Win32. – David Heffernan Sep 24 '13 at 22:30
  • @c00000fd Question is way too broad. Printing is an enormous topic. It's really hard to do well. Get a library. Or curl up with Petzold and work it out from scratch. Nothing in C++ can help that's about 6 abstraction levels beneath printing. – David Heffernan Sep 24 '13 at 22:31
  • @c00000fd: The "right way" to print from a Win32 program is to use a Win32 printing device context. Just like any other controls, IE controls should be used when you need IE functionality in your program. Including an IE control just to make forms and printing slightly easier is a little crazy, especially when, as you're finding, it often doesn't make printing easier at all. – Crowman Sep 24 '13 at 22:37
  • @SigTerm: Writing Windows applications is inherently platform-specific, so that shouldn't be a cause for concern. – Crowman Sep 24 '13 at 22:38
  • @DavidHeffernan: Why is it a broad question? Did I not specify that I need to print forms, such as accounting reports, receipts, etc. (I just realized that the term `form` means `dialog window` in programming terms, and that's *not* what I'm inquiring about. I apologize for that omission.) – c00000fd Sep 24 '13 at 23:13
  • @PaulGriffiths: You don't have to use platform-specific API to write Windows application. Also, if you're making commercial application, being locked into single platform is a bad thing. – SigTerm Sep 25 '13 at 12:38
  • @DavidHeffernan: You can use cross-platform API that has printer support. It doesn't matter what it uses under the hood on windows platform. I though this is obvious. – SigTerm Sep 25 '13 at 12:39
  • @SigTerm If you only want to target one platfor, where's harm in using a paltform specific library? – David Heffernan Sep 25 '13 at 12:56
  • @DavidHeffernan: Because it is very likely that at some point later you'll decide that you want to support another platform. Obviously, at this point you'll have a huge codebase and rewriting everything in cross-platform fashion will be a major pain. Too risky. – SigTerm Sep 25 '13 at 13:02
  • @SigTerm: There are an awful lot of software companies that have made an awful lot of money writing and selling platform-specific software. Using any library limits you to platforms on which that library has been implemented, so it's never a question of avoiding platform-specific code, just a question of *how* specific you do or do not want to be. Portability is generally a good goal, but it is just one goal, and one that often co-exists with other competing goals. – Crowman Sep 25 '13 at 22:52

3 Answers3

1

If I remember correctly, printers have their own HDC, and you can draw on it. That'll work if have something simple. If you want to render HTML page using pure WinAPI, you're in big trouble.

I'd advise to abandon winapi and try GUI framework instead.

Qt 4(and 5, most likely) has text editor that can display rich text, layout engine for rich text, component that can display web pages. Read documentation a bit, and you will most likely find a way to render web page onto printer instead of screen. So far it looks like exactly what you would need.

Using Qt will add dependencies (20+ MB of DLLs for your project), but, IMO< it is a better idea than trying to use IE COM interfaces.

If you don't want to use Qt, you could try something like WebKIT, but I had some bad experiences with it, plus Qt might be just easier to use.

Additional info on printing: Printing with Qt.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
0

Try searching for GDI, if you want to use win32 builtins.

Or use another toolkit like wxWidgets. Or consider writing to PDF with some library. Or let LaTeX do the heavy lifting - writing text files is easy. The LaTeX-way works as long as you don't want to modify your output depending on the layout (one Use-Case that doesn't work with LaTeX is the "balance" at the top/bottom of each page.)

Sebastian
  • 1,839
  • 12
  • 16
  • 1
    Or, or, or ... -> too broad! – πάντα ῥεῖ Sep 24 '13 at 21:53
  • wxWidgets is too broad of a subject as well. Anything in particular? As GDI is concerned -- are you really suggesting me implementing HTML+CSS functionality with it? – c00000fd Sep 24 '13 at 22:08
  • "Try searching for GDI, if you hate yourself." I kid, I kid. – Taylor Brandstetter Sep 24 '13 at 22:08
  • @c00000fd: No, I'm suggesting to implement "forms -- nothing fancy, just plain text, with lines, tables, and simple graphics." with it. Just your Use-Case, not a general formatting solution. About wxWidgets: There's documentation and example programs about printing. On about the same level of abstraction as GDI, just a little bit simpler. – Sebastian Sep 24 '13 at 22:12
  • @Sebastian: Yes, obviously I can hard-code all of the markup into my C++ logic using plain GDI. That is a very ugly solution though, that will be my last resort if I don't find any other way of doing it... – c00000fd Sep 24 '13 at 22:15
  • @c00000fd: So, instead of hard-coding the markup, you want some markup language, but you don't want HTML because that's not really invented for output on paper... that's why I suggested LaTeX. I have good experience with that (except for the "balance" thing which needed an ugly workaround). Always printing onto the same paper size, though. – Sebastian Sep 24 '13 at 22:22
-1

Consider having your program generate XML files and using XSLT to render them into HTML.

By attaching stylesheets you will make it much easier to customize the presentation.

Luis
  • 1,235
  • 1
  • 12
  • 12
  • Generating HTML is the one way c00000fd already tried, with bad success. Adding an XSLT step before that doesn't solve the IE problems. Stylesheets maybe do. – Sebastian Sep 24 '13 at 21:59
  • @Luis: Generation & preview of the markup for my forms is not a problem. I can currently do so with HTML+CSS. The issue is printing it. And namely providing the ability to set paper size, margins, etc. – c00000fd Sep 24 '13 at 22:09
  • You can print the XMLs from IE and specify margin sizes in CSS, which is why I suggested to consider it. – Luis Sep 25 '13 at 19:44
  • ...and page margins can be configured in the registry: http://support.microsoft.com/kb/313723 and http://support.microsoft.com/kb/236777/EN-US . I agree is not a perfect solution because they affect the user's IE settings and other settings cannot be controlled, but I think @c00000fd is surely better off with the current strategy instead of working from scratch using GDI. – Luis Sep 25 '13 at 19:58