www.pudn.com > doc2pdf-0_7_1.rar > doc2pdf_convert.cpp


///////////////////////////////////////////////////////////////////////////// 
//  
// Project:		Doc2pdf 
// 
// File:		doc2pdf_converter.h 
// 
// Author(s):	Matt Peterson  
// 
// Description:	Email converter 
// 
///////////////////////////////////////////////////////////////////////////// 
 
 
#include "stdafx.h" 
#include "doc2pdf.h" 
#include "doc2pdf_converter.h" 
#include "base64codec.h" 
#include "printablecodec.h" 
#include "doc2pdf_keystroke.h" 
#include "doc2pdf_log.h" 
 
#ifndef DOC2PDF_VERSION 
#error You need to define DOC2PDF_VERSION 
#endif 
 
//--------------------------------------------------------------------------- 
// The following is a list of expected Window title strings you may need 
// to translate the some of them to the language that is supported on the 
// machine that will be running Doc2pdf 
//--------------------------------------------------------------------------- 
#define DOC2PDF_CONVERT_PPT_PRINT_TITLE			"Print" 
#define DOC2PDF_CONVERT_PPT_PRINT_TO_FILE_TITLE	"Print To File" 
#define DOC2PDF_CONVERT_PPT_PRINT_STATUS_TITLE  "Print Status" 
#define DOC2PDF_CONVERT_XLS_VIEWER_TITLE		"Microsoft Excel Viewer" 
#define DOC2PDF_CONVERT_XLS_PRINT_TO_FILE_TITLE	"Print To File" 
#define DOC2PDF_CONVERT_DOC_VIEWER_PREFIX_TITLE	"Microsoft Word Viewer 97 - " 
#define DOC2PDF_CONVERT_DOC_VIEWER_TITLE		"Microsoft Word Viewer 97" 
#define DOC2PDF_CONVERT_DOC_PASSWORD_TITLE      "Password" 
 
#define DOC2PDF_CONVERT_LOTUS_VIEWER_PREFIX_TITLE "KeyView for Lotus - " 
#define DOC2PDF_CONVERT_LOTUS_VIEWER_TITLE        "KeyView for Lotus" 
#define DOC2PDF_CONVERT_LOTUS_PRINT_TITLE		  "Print" 
#define DOC2PDF_CONVERT_LOTUS_PRINT_TO_FILE_TITLE "Print To File" 
 
 
#ifndef CRLF  
#define CRLF "\r\n" 
#endif 
 
#define DOC2PDF_LOAD_TIMEOUT		300	// 5 minutes max to load viewed doc 
 
// BEGIN IMPORTANT COPYRIGHT NOTICE! 
// You may not change the following #define constant or otherwise modify 
// the doc2pdf code so that the text is not displayed in every conversion  
// reply.   
// 
// Doc2pdf is free software which means that the author does not received  
// any monitary compensation for the effort that is required to provide you  
// with this software.  The only restriction to free use and distribution 
// of this software is that you do not erase or modify the attribution of  
// this effort. 
// 
// If you would like special license to remove the following text from 
// converted messages, please contact Matthew Peterson via email at 
// mpeterson@users.sourceforge.net.  The cost of the unlimitied license 
// (erasing all attribution of the author so the Doc2pdf looks like your 
// own) is $50 USD. 
#define DOC2PDF_GREETING \ 
"The documents converted by Doc2pdf version " DOC2PDF_VERSION CRLF CRLF\ 
"Doc2pdf is OpenSource Software written by Matthew Peterson.  For more " CRLF\ 
"information please see http://doc2pdf.sourceforge.net" CRLF CRLF 
// 
// 
// End of IMPORTANT COPYRIGHT NOTICE! 
 
#define DOC2PDF_NOCONVERT \ 
"Could not find any convertable attachments.  If you are sure that you " CRLF \ 
"did indeed attach a file to convert, please report a Doc2pdf bug by " CRLF \ 
"by sending EXACTLY the same email to mpeterson@users.sourceforge.net with the"CRLF \ 
"the the string \"doc2pdf bug\" in the subject line." CRLF \ 
CRLF \ 
"Before reporting a bug, please note: " CRLF \ 
CRLF \ 
"    - Doc2pdf only converts attachments that are sent as part of MIME messages"CRLF \ 
"      Some mailers do not send MIME format" CRLF \ 
"    - Doc2pdf can not convert password protected documents (it doesn't know " CRLF \ 
"      the password)."CRLF \ 
CRLF 
 
#define DOC2PDF_ORIGMSG CRLF "-------- Original Message --------" CRLF CRLF 
 
#define DOC2PDF_GS_AT \ 
"-dCompatibilityLevel#%s -q -dSAFER -r600 -dNOPAUSE -dBATCH -sDEVICE#pdfwrite "\ 
"-sOutputFile#\"%s\" -c .setpdfwrite -f \"%s\"" 
 
#define DOC2PDF_GS_COMPATLEVEL      "1.3" 
 
 
// Convert a file from Lotus Smart Suite to PostScript 
int Doc2pdfConverter::LotusToPs(Doc2pdfAttachment& attachment) 
{ 
	HINSTANCE	hmod			= NULL; 
	HWND		viewerWindow	= NULL; 
	HWND		printWindow	    = NULL; 
	CString		keystrokes; 
	CString		pspath; 
	CString		title; 
	int			i; 
	 
	// Generate the new PS path 
	pspath = attachment.m_Path.Left(attachment.m_Path.ReverseFind('.')); 
	pspath += ".ps";	 
	Doc2pdfTightyString(pspath); 
			 
	// Open the viewer 
	hmod = ShellExecute(NULL,"open",attachment.m_Path,NULL,NULL,SW_SHOWDEFAULT); 
	if((DWORD)hmod <= 32) 
	{ 
		goto FAILURE;	 
	} 
 
	title = DOC2PDF_CONVERT_LOTUS_VIEWER_PREFIX_TITLE; 
	title += attachment.m_Path.Right(attachment.m_Path.GetLength() - attachment.m_Path.ReverseFind('\\') - 1); 
	 
	// Wait for the viewer to come up 
	i=0; 
	Sleep(1000); // 1 second sleep			 
 	while((viewerWindow = FindWindow(NULL, title)) == NULL) 
	{ 
		if(i == DOC2PDF_LOAD_TIMEOUT) 
		{ 
			goto FAILURE; 
		} 
		Sleep(5000); // 1 second sleep 
		i++; 
		 
		viewerWindow = FindWindow(NULL, DOC2PDF_CONVERT_LOTUS_VIEWER_TITLE); 
		if(viewerWindow) 
		{ 
			// Could not load the specified file 
			goto FAILURE; 
		} 
	} 
	 
	// Send keystrokes 
	// CTRL - P for print 
	for(i=0;i<10;i++) 
	{ 
		Sleep(1000); 
		keystrokes="#CTLP#"; 
		Doc2pdfSendKeyStrokes(keystrokes); 
		 
		if(Doc2pdfWaitForWindow(hmod,  
			                    DOC2PDF_CONVERT_LOTUS_PRINT_TITLE, 
								1) == TRUE) 
		{ 
			break;		 
		} 
	} 
		  
 	// Send keystrokes 
	// type ENTER to print and type file to print to, then ENTER 
	keystrokes.Format("\n%s\n",pspath); 
	Doc2pdfSendKeyStrokes(keystrokes); 
	// Wait DOC2PDF_LOAD_TIMEOUT seconds for the file to be created and writeable 
	for(i=0;i %s\n", 
		          attachment.m_Name, 
				  attachment.m_Name.Left(attachment.m_Name .ReverseFind('.')) + ".pdf"); 
 
	return 0; 
 
 
NOT_CONVERTED: 
 
	output.Format("ERROR: Unable to convert %s to .pdf format. It is probable that" CRLF 
		          "the attachment is not a valid document or password protected." CRLF, 
		          attachment.m_Name); 
	return -1; 
} 
 
// Convert email 
int Doc2pdfConverter::ConvertEmail(Doc2pdfEmail& email) 
{ 
	POSITION			pos; 
	Doc2pdfAttachment*	attachment;	 
	CString				output; 
		 
	// Add the original message delimiter 
	email.m_Body.Insert(0,DOC2PDF_ORIGMSG); 
	 
	if(email.m_Attachments.GetCount()) 
	{ 
		// Convert all attachments 
        pos = email.m_Attachments.GetHeadPosition(); 
		while(pos) 
		{ 
			attachment = email.m_Attachments.GetAt(pos); 
			 
			ConvertAttachment(*attachment,output); 
 
			email.m_Body.Insert(0,output); 
			 
			email.m_Attachments.GetNext(pos); 
		} 
	} 
	else 
	{ 
		// No attachments to convert 
		email.m_Body.Insert(0,DOC2PDF_NOCONVERT); 
	}	 
	 
	// Add the Doc2pdf greeting to the beginning 
	email.m_Body.Insert(0,DOC2PDF_GREETING); 
 
	return 0;  
} 
 
int Doc2pdfConverter::Init(const CString& ghostScriptDir) 
{ 
	m_GhostScriptDir = ghostScriptDir; 
	return 0; 
} 
 
// Returns number of emails converted. 
int Doc2pdfConverter::ConvertAllEmail(Doc2pdfEmailList& emaillist) 
{ 
	Doc2pdfEmail*	email; 
	POSITION		pos; 
	int				result = 0; 
 
	pos = emaillist.GetHeadPosition(); 
	while(pos) 
	{ 
		email = emaillist.GetAt(pos); 
		if(ConvertEmail(*email) == 0) 
		{ 
			result ++; 
		} 
		 
		emaillist.GetNext(pos); 
	} 
	 
	return result; 
}