最近有个需求需要对报告打印进行统一的管理,最终实现方案如下:
1、安装Microsoft Print To PDF虚拟打印机,该打印机可以将所有打印数据转换为PDF
2、通过Microsoft Print To PDF虚拟机参数复制一台新的虚拟打印机
3、创建打印输出端口,指定输出路径
4、设置新虚拟打印机的端口为新创建的端口。
安装Microsoft Print To PDF
Microsoft Print To PDF属于Windows可选功能,可以借助 dism.exe进行安装布署。如下:
1 dism /Online /Enable-Feature /FeatureName:"Printing-PrintToPDFServices-Features" /NoRestart /Quiet
使用CreateProcess函数执行dism.exe
1 #include<Windows.h> 2 #include<tchar.h> 3 4 BOOL InstallMicrosoftPrintToPDF() 5 { 6 LPWSTR szCmd = _tcsdup(LR"(dism /Online /Enable-Feature /FeatureName:"Printing-PrintToPDFServices-Features" /NoRestart /Quiet)"); 7 STARTUPINFO si{}; 8 PROCESS_INFORMATION pi{}; 9 si.cb = sizeof(si); 10 auto nRet = CreateProcess(NULL, szCmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); 11 12 if (!nRet) 13 { 14 if (pi.hThread) 15 { 16 CloseHandle(pi.hThread); 17 } 18 19 if (pi.hProcess) 20 { 21 CloseHandle(pi.hProcess); 22 } 23 } 24 25 free(szCmd); 26 return nRet; 27 }
然后调用OpenPrinter函数打开打印机
标签:打印机,NULL,C++,虚拟,Print,PDF,pi,dism From: https://www.cnblogs.com/zhaotianff/p/17271426.html