Converting any file to PDF

  • Thread starter Thread starter Subhash Badri
  • Start date Start date
S

Subhash Badri

Hi,
I am trying to convert any document into PDF format using VC++ MFC.
I am using ghost view to convert .PS to .PDF.
I have written a code for converting any file to .ps by reading the input
file and printing that file to a file with .ps as extension. The problem I
am getting is the .PS is not of correct format, hence the conversion from
..ps to .pdf is failing.
Can any one help me with an idea and how to go bout with this?
thanks in advance.

rgds
subhash
 
In the code you use to convert to ps, are you "printing" with the Acrobat
Distiller virtual printer instance?

Adrian
 
Nope,
Check out the code anyways.

CString szDeviceName;
CPrintDialog dlg(FALSE);
// dlg.DoModal();

if (!dlg.GetDefaults())
AfxMessageBox(_T("You have no default printer!"));
else
szDeviceName = dlg.GetDeviceName();

HANDLE hPrinter;
DOC_INFO_1 DocInfo;
DWORD dwJob;
DWORD dwBytesWritten;

// Need a handle to the printer.
if( ! OpenPrinter( szDeviceName.GetBuffer(0), &hPrinter, NULL ) )
return FALSE;

CString szPostscript = szOutputFile.Left(szOutputFile.ReverseFind('.'))
+ ".pdf";
CString szOFile;
szOFile.Format("%s%s%s", this->m_szDocPath, "\\", szPostscript);
// Fill in the structure with info about this "document."
DocInfo.pDocName = this->m_szDocNameWithPath.GetBuffer(0);
DocInfo.pOutputFile = szOFile.GetBuffer(0);
DocInfo.pDatatype = "raw";
// Inform the spooler the document is beginning.
if( (dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo )) == 0 )
{
ClosePrinter( hPrinter );
return FALSE;
}
// Start a page.
if( ! StartPagePrinter( hPrinter ) )
{
EndDocPrinter( hPrinter );
ClosePrinter( hPrinter );
return FALSE;
}

CFile oFile(this->m_szDocNameWithPath.GetBuffer(0), CFile::modeRead);
DWORD dwCount = oFile.GetLength();
char *lpData = (char*) malloc (dwCount * sizeof(char));
oFile.ReadHuge(lpData, dwCount);
// Send the data to the printer.
if( !WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
{
EndPagePrinter( hPrinter );
EndDocPrinter( hPrinter );
ClosePrinter( hPrinter );
return FALSE;
}
// End the page.
if( ! EndPagePrinter( hPrinter ) )
{
EndDocPrinter( hPrinter );
ClosePrinter( hPrinter );
return FALSE;
}

// Inform the spooler that the document is ending.
if( ! EndDocPrinter( hPrinter ) )
{
ClosePrinter( hPrinter );
return FALSE;
}
// Tidy up the printer handle.
ClosePrinter( hPrinter );
// Check to see if correct number of bytes were written.
if( dwBytesWritten != dwCount )
return FALSE;
return TRUE;
 
Hi Subhash,

I'm not a programmer, so your code doesn't mean much to me. However, it
looks like your code calls the default printer to create the ps file. I
don't know which printer this is in your case, but Adobe recommends that
postscript files always be printed with the Acrobat Distiller printer
instance (having previously set the preferences to download TrueType graphic
fonts as softfonts, to download document fonts as NativeType and to optimize
the postscript output for portability). If you can get hold of the Distiller
printer driver I would try that first. The driver is installed when you
install Acrobat (commercial version)...I'm not sure if you can get hold of
it separately. (Failing that, you could try printing to another printer.)

Adrian
 
Back
Top