• ×
    Information
    Need Windows 11 help?
    Check documents on compatibility, FAQs, upgrade information and available fixes.
    Windows 11 Support Center.
  • post a message
  • ×
    Information
    Need Windows 11 help?
    Check documents on compatibility, FAQs, upgrade information and available fixes.
    Windows 11 Support Center.
  • post a message
Guidelines
Here is the solution to configure HP FutureSmart Printers with Kiwi Syslog Server Click here to view the instructions!
Check some of the most frequent questions about Instant Ink: HP INSTANT INK, HP+ PLANS: INK AND TONER.


Check out our WINDOWS 11 Support Center info about: OPTIMIZATION, KNOWN ISSUES, FAQs AND MORE.
HP Recommended
HP Color LaserJet Pro MFP 4302fdw Printer

Hello,

 

I would like to print on Windows using C++ a PDF file on two-sided (duplex), for this I use :

 

 

#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <fstream>

LPDEVMODE GetLandscapeDevMode(HANDLE hPrinter, LPTSTR pDevice)
{
    LPDEVMODE pDevMode;
    DWORD dwNeeded, dwRet;
    
    /*
    * Step 1:
    * Allocate a buffer of the correct size.
    */ 
    dwNeeded = DocumentProperties(NULL,
    hPrinter, /* Handle to our printer. */ 
    pDevice, /* Name of the printer. */ 
    NULL, /* Asking for size, so */ 
    NULL, /* these are not used. */ 
    0); /* Zero returns buffer size. */ 
    pDevMode = (LPDEVMODE)malloc(dwNeeded);
    
    /*
    * Step 2:
    * Get the default DevMode for the printer and
    * modify it for your needs.
    */ 
    dwRet = DocumentProperties(NULL,
    hPrinter,
    pDevice,
    pDevMode, /* The address of the buffer to fill. */ 
    NULL, /* Not using the input buffer. */ 
    DM_OUT_BUFFER); /* Have the output buffer filled. */ 
    if (dwRet != IDOK)
    {
        /* If failure, cleanup and return failure. */ 
        free(pDevMode);
        ClosePrinter(hPrinter);
        return NULL;
    }
    
    /*
    * Make changes to the DevMode which are supported.
    */ 
    if (pDevMode->dmFields & DM_ORIENTATION)
    {
        /* If the printer supports paper orientation, set it.*/ 
        pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
    }
    
    if (pDevMode->dmFields & DM_DUPLEX)
    {
        /* If it supports duplex printing, use it. */ 
        pDevMode->dmDuplex = DMDUP_HORIZONTAL;
        std::wcerr << L"supports duplex printing" << std::endl;
    }
    
    /*
    * Step 3:
    * Merge the new settings with the old.
    * This gives the driver an opportunity to update any private
    * portions of the DevMode structure.
    */ 
    dwRet = DocumentProperties(NULL,
    hPrinter,
    pDevice,
    pDevMode, /* Reuse our buffer for output. */ 
    pDevMode, /* Pass the driver our changes. */ 
    DM_IN_BUFFER | /* Commands to Merge our changes and */ 
    DM_OUT_BUFFER); /* write the result. */ 
    
    /* Finished with the printer */ 
    //ClosePrinter(hPrinter);
    
    if (dwRet != IDOK)
    {
        /* If failure, cleanup and return failure. */ 
        free(pDevMode);
        return NULL;
    }
    
    /* Return the modified DevMode structure. */ 
    return pDevMode;
}

void PrintPDF(LPTSTR printerName, char* bytes, int numBytes) {
    HANDLE hPrinter;
    PRINTER_DEFAULTS pd = { NULL, NULL, PRINTER_ACCESS_USE };
    if (!OpenPrinter(printerName, &hPrinter, &pd)) {
        std::wcerr << L"Failed to open printer: " << printerName << std::endl;
        return;
    }
    
    LPDEVMODE pDevMode = NULL;
    pDevMode = GetLandscapeDevMode(hPrinter, printerName);
    if (!pDevMode) {
        std::cerr << "Failed to set printer settings." << std::endl;
        ClosePrinter(hPrinter);
        return;
    }
    
    std::wcout << L"SetPrinter ok\n";
    
    // Create a DOC_INFO_1 structure
    DOC_INFO_1 docInfo;
    docInfo.pDocName = (LPTSTR)L"PDF Document";
    docInfo.pOutputFile = NULL;
    docInfo.pDatatype = (LPTSTR)L"RAW";

    // Start a print job
    DWORD dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&docInfo);
    if (dwJob == 0) {
        std::cerr << "Failed to start print job." << std::endl;
        ClosePrinter(hPrinter);
        return;
    }

    // Start a page
    if (StartPagePrinter(hPrinter) == 0) {
        std::cerr << "Failed to start page." << std::endl;
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return;
    }

    // Write the PDF data to the printer
    DWORD bytesWritten;
    if (WritePrinter(hPrinter, bytes, numBytes, &bytesWritten) == 0) {
        std::cerr << "Failed to write to printer." << std::endl;
        EndPagePrinter(hPrinter);
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return;
    }

    // End the page
    if (EndPagePrinter(hPrinter) == 0) {
        std::cerr << "Failed to end page." << std::endl;
    }

    // End the print job
    if (EndDocPrinter(hPrinter) == 0) {
        std::cerr << "Failed to end print job." << std::endl;
    }
    
    std::cerr << "Impression envoyée." << std::endl;
    // Close the printer
    ClosePrinter(hPrinter);
}

std::vector<char> readPDFToByteArray(const std::string& filePath) {
    // Open the file in binary mode
    std::ifstream file(filePath, std::ios::binary | std::ios::ate);
    if (!file.is_open()) {
        throw std::runtime_error("Could not open file");
    }

    // Get the size of the file
    std::streamsize fileSize = file.tellg();
    file.seekg(0, std::ios::beg);

    // Read the file contents into a byte array
    std::vector<char> buffer(fileSize);
    if (!file.read(buffer.data(), fileSize)) {
        throw std::runtime_error("Error reading file");
    }

    return buffer;
}

int main()
{
    const wchar_t* printerName = L"my printer name";

    std::vector<char> pdfBytes;
    try {
        std::string filePath = "C:\\path\\doc.pdf";
        pdfBytes = readPDFToByteArray(filePath);
        std::cout << "PDF file read successfully. Size: " << pdfBytes.size() << " bytes" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    PrintPDF((LPTSTR)printerName, pdfBytes.data(), (int)pdfBytes.size());
    
    return 0;
}

 

 

 
but my HP printer prints one sided.
Is this due to a driver problem ?
 
Thanks
5 REPLIES 5
HP Recommended

I have a suggestion. Do a second install of the printer you can call it "CopyOne" if you want.

 

In windows, set the printer property default of "CopyOne" to be duplex.

 

Use your application on the second printer, set a break point and look at the differences in the properties and make adjustments to your app so it works with the original printer.

 

I would also use C# and ask ChatGpt for help


Thank you for using HP products and posting to the community.
I am a community volunteer and do not work for HP. If you find
this post useful click the Yes button. If I helped solve your
problem please mark this as a solution so others can find it
HP Recommended

That's is trange, I set the printer property default of "CopyOne" to be duplex but the printer print one sided ???

HP Recommended

Yea, something is wrong.  Try to get duplex working in windows before using your APP.

 

Once it is working then just have your program read the properties (do not set any properties) and compare it to what you think they should be for duplex to work and make the changes in  your app.


Thank you for using HP products and posting to the community.
I am a community volunteer and do not work for HP. If you find
this post useful click the Yes button. If I helped solve your
problem please mark this as a solution so others can find it
HP Recommended

Using MS Word or Adobe Reader Duplex mode work, it's only using StartDocPrinter() not working, it seems using a basic settings and not default settings, it's impossible to use it.

It's only possible to get default settings.

HP Recommended
DocumentProperties(NULL
    hPrinter,
    pDevice,
    pDevMode, /* Reuse our buffer for output. */ 
    pDevMode, /* Pass the driver our changes. */ 
    DM_IN_BUFFER | /* Commands to Merge our changes and */ 
    DM_OUT_BUFFER); /* write the result. */

Le DEVMODE is passed to DocumentProperties() which is stored in the spool file.

but it's ignored by « Hp Smart Universal Printing » driver...

† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the <a href="https://www8.hp.com/us/en/terms-of-use.html" class="udrlinesmall">Terms of Use</a> and <a href="/t5/custom/page/page-id/hp.rulespage" class="udrlinesmall"> Rules of Participation</a>.