R
Richard Guion
I have an MFC Win32 client application that needs to generate PDF
files. The easiest way to do it is to have our customers purchase and
install Adobe Acrobat, and then our program will use the "Adobe PDF" printer
driver to output our text to PDF files.
From any application like Notepad, Word, Excel, using the Adobe PDF writer
works fine when I do File\Print, click "Print to File", uncheck the printer
property of downloading fonts to printer--my PDF file is created fine.
I need to do this from C++. The user is not going to use File\Print in our
app, we need to convert files as we chug online in certain operations.
Something is not working in my code which I've pasted below. I'm using
OpenPrinter
to open the "Adobe PDF" printer (return code is good), I call
StartDocPrinter setting
my output file name to "D:\temp\test.pdf" and set the datatype either to RAW
or NULL,
same result occurs: a test.pdf file is created, but it is pure text "this is
a test"
instead of PDF content.
Anyone see what I am doing wrong? It appears to me that I need another code
for
pDatatype. Or should I not be using these OS calls for a printer driver?
=========================
#include <winspool.h>
....
BOOL PrintStringDirect( LPCSTR Str, LPTSTR PrinterName, LPSTR DeviceName )
{
BOOL bRet = FALSE;
HANDLE hPrinter;
if ( OpenPrinter( PrinterName, &hPrinter, NULL ) )
{
DOC_INFO_1 doc_info = {0};
doc_info.pDocName = "Test Document";
doc_info.pOutputFile = DeviceName;
//doc_info.pDatatype = (LPTSTR) NULL;
doc_info.pDatatype = "raw";
DWORD jobid = StartDocPrinter( hPrinter, 1, (LPBYTE)&doc_info );
if ( jobid != 0 )
{
DWORD written;
DWORD dwNumBytes = lstrlen( Str );
WritePrinter( hPrinter, (void*) Str,
dwNumBytes, &written );
if ( written == dwNumBytes )
bRet = TRUE;
}
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
}
return bRet;
}
PrintStringDirect("This is a test", "Adobe PDF", "D:\\temp\\test.pdf");
files. The easiest way to do it is to have our customers purchase and
install Adobe Acrobat, and then our program will use the "Adobe PDF" printer
driver to output our text to PDF files.
From any application like Notepad, Word, Excel, using the Adobe PDF writer
works fine when I do File\Print, click "Print to File", uncheck the printer
property of downloading fonts to printer--my PDF file is created fine.
I need to do this from C++. The user is not going to use File\Print in our
app, we need to convert files as we chug online in certain operations.
Something is not working in my code which I've pasted below. I'm using
OpenPrinter
to open the "Adobe PDF" printer (return code is good), I call
StartDocPrinter setting
my output file name to "D:\temp\test.pdf" and set the datatype either to RAW
or NULL,
same result occurs: a test.pdf file is created, but it is pure text "this is
a test"
instead of PDF content.
Anyone see what I am doing wrong? It appears to me that I need another code
for
pDatatype. Or should I not be using these OS calls for a printer driver?
=========================
#include <winspool.h>
....
BOOL PrintStringDirect( LPCSTR Str, LPTSTR PrinterName, LPSTR DeviceName )
{
BOOL bRet = FALSE;
HANDLE hPrinter;
if ( OpenPrinter( PrinterName, &hPrinter, NULL ) )
{
DOC_INFO_1 doc_info = {0};
doc_info.pDocName = "Test Document";
doc_info.pOutputFile = DeviceName;
//doc_info.pDatatype = (LPTSTR) NULL;
doc_info.pDatatype = "raw";
DWORD jobid = StartDocPrinter( hPrinter, 1, (LPBYTE)&doc_info );
if ( jobid != 0 )
{
DWORD written;
DWORD dwNumBytes = lstrlen( Str );
WritePrinter( hPrinter, (void*) Str,
dwNumBytes, &written );
if ( written == dwNumBytes )
bRet = TRUE;
}
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
}
return bRet;
}
PrintStringDirect("This is a test", "Adobe PDF", "D:\\temp\\test.pdf");