How to print a Html file with c++

Hello my name is alex and I'm from France so sorry for possible mistakes.

I developed a programm in c++ for a gite de france in order to create contracts everything fine but I can't fine how to print html files in c++ ?
So please anyone have a solution ?

I tried system("print test.html") or system("print COM1 test.html") bit it doesn't work...

I use Windows 7 64 bits and the printer is an hp

Thank you for your help

Alex
When you say "print", do you mean
(a) send plaintext HTML to a printer,
(b) send rendered HTML to a printer,
(c) display the text of the html file to the console, or
(d) display HTML rendered as a web browser would?

(c) is by far the simplest of these three options.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    using namespace std;
    
    ifstream html("test.html");
    if (!html)
    {
        cout << "Error opening test.html\n";
        return 1;
    }
    
    string line;
    while (getline(html, line))
    {
        cout << line << '\n';   
    }
    
    return 0;
}


The other two options require knowledge outside of C++ itself.

PS: You accidentally double-posted. You can delete the other topic to avoid annoying other users.
Last edited on
First of all thank you for your quick answer!

in fact I think I made a mistake in the explanation because what I mean is printing on a printer on paper...

Thank you ;-)
That still leaves gando's choices (a) and (b).
Please be specific.
So it's choice (b)

but in fact the contract are made in html but I think they can be converted in pdf so if it is easier to print a pdf in c++ it would be ok too ;-)
Alex
Last edited on
the dos print command is really just for text. You can use it, but your printer needs a com port to be set (printer properties, ports) (these used to be physical plugs on computers, they are virtual now) before it will go. Slightly easier, notepad /p file will use the default printer for a text file. Other programs may support this as well, you can see if one of the browsers has a similar option.

To do it in code (vs letting some other program do it for you) rendered html is going to require you to write some GUI specific code to talk to the installed printer from the common interface -- not strict C++ code, but library calls.

consider:
http://www.printhtml.com/
Last edited on
I was looking into that print program. It seems really old and possibly defunct. I couldn't get it to work; I doubt it knows how to handle rendering HTML.

The main problem is that you need some sort of middleman to tell you how to render the HTML to a printer. For example, you can force an internet explorer pop-up and then print from there. In WinForms this can be accessed with a WebBrowser control.

There's a project on GitHub that apparently uses the QtWebKit to assist in printing:
https://github.com/kendallb/PrintHtml
But that requires Qt, which is a pretty heavy dependency.
(Not sure if this is the same "print html" as jonnin's link. I think it's different)
The easiest way to print a rendered HTML document is load the file into a browser and use the "print page" option. If the web page(s) are in PDF format use a PDF viewer and print the PDF.
yea it says win vista... I didn't try it, but web design has changed too much, even if it worked on older pages.


To clarify, by "that print program" I meant the built-in Windows one. I didn't check printhtml.com's program.
thank you for all your answers !!!

i tried wkhtmltopdf to first convert my html files to pdf files; it works fine but in the new created pdf files the pictures and the objects called frome other txt files are empty the convertor didn't convert them...

i tried to add :
wkhtmltopdf L:\\private\\Contratformauto.html c:\\LC\\contrat.pdf [ --allow <L:\\private>]"

to autorize the software to acces to image and text situated in .txt but it says acces denied ?

does anybody have an idea ? i tried work as administrator too but nothing..

;-)
did you use the administrator version of the console (cmd)? (batch file, or just typing direct)
Or are these system calls in your c++? (in which case, try running the c++ program in admin console).
Last edited on
In fact I changed the version of wkhtmltopdf because the old versions didn't have the problem ...
It's strange because I was in admin mode and clear all restrictions to access the files doesn't matter I will use the old version 😉
I think it pretty clear OP wants to print the rendered HTML just as if you were to print it from your web browser.

Unfortunately, what you want to do is a fairly complicated task, and one that involves OLE/COM objects, etc.

Fortunately, you are not the only person with this problem. And someone created a solution. Check it out:

https://stackoverflow.com/questions/3934508/printing-a-formatted-html-page-in-c-sharp

Hope this helps.
How do you feel about using Qt?
https://stackoverflow.com/questions/59274653/how-to-print-from-qwebengineview

The answer in the above link says to load the html into the qWebEngineView, wait for an event that says that the loading is completed, then convert it to a PDF, finally print the PDF. The good news is that it's all controlled withing Qt, so no more system() calls.
Last edited on
I think the better question here is how OP's employer feels about using Qt, as there are some very hairy sticking points to using that in a business environment.
Last edited on
Topic archived. No new replies allowed.