ghostscript如何设置静默打印
时间: 2024-06-04 10:11:13 浏览: 472
静默打印是指打印过程中不显示打印进度和提示信息,直接进行打印。可以通过以下步骤实现Ghostscript的静默打印:
1. 打开命令行窗口,进入Ghostscript安装目录。
2. 执行以下命令,实现静默打印:
gswin32c.exe -dNOPAUSE -dBATCH -dNOPROMPT -dPrinted -dNOPRINT -sDEVICE=printer -sOutputFile="%printer%printer_name" input_file
其中,printer_name为打印机名称,input_file为要打印的文件路径。
3. 执行完毕后,文件将被发送到指定打印机进行打印,期间不会有任何提示信息。
注意事项:
1. 需要确保打印机已经安装并且设置为默认打印机。
2. 如果要打印多个文件,可以使用通配符*来代替input_file,例如:
gswin32c.exe -dNOPAUSE -dBATCH -dNOPROMPT -dPrinted -dNOPRINT -sDEVICE=printer -sOutputFile="%printer%printer_name" *.pdf
这将打印当前目录下所有的PDF文件。
3. 如果要使用Ghostscript进行打印,需要安装Ghostscript软件。可以从Ghostscript官网下载安装程序进行安装。
相关问题
windows下c++使用ghostscript的api接口静默打印pdf
使用Ghostscript的API接口可以在Windows下实现静默打印PDF文件。Ghostscript是一个开源的PDF处理引擎,它提供了C/C++接口,可以在程序中调用相应的函数实现PDF打印功能。
下面是一个简单的示例代码,演示如何使用Ghostscript的API接口实现静默打印PDF文件:
```c++
#include <stdio.h>
#include <windows.h>
#include "gdiplus.h"
#include "gs/gsapi.h"
#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "gsdll32.lib")
void print_pdf(const wchar_t* filename, const wchar_t* printer_name)
{
// 初始化Ghostscript API
int argc = 3;
const char* argv[3] = {
"gsdll32.dll",
"-dNOPAUSE",
"-dBATCH"
};
GSAPI_HANDLE gs_handle;
gsapi_new_instance(&gs_handle, NULL);
// 设置输出打印机
char printer[256] = { 0 };
char port[256] = { 0 };
WideCharToMultiByte(CP_ACP, 0, printer_name, -1, printer, sizeof(printer), NULL, NULL);
PRINTER_INFO_2* pinfo = NULL;
DWORD num_printers = 0;
EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &num_printers, NULL);
pinfo = (PRINTER_INFO_2*)malloc(num_printers * sizeof(PRINTER_INFO_2));
EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)pinfo, num_printers * sizeof(PRINTER_INFO_2), &num_printers, NULL);
for (DWORD i = 0; i < num_printers; ++i) {
if (strcmp(pinfo[i].pPrinterName, printer) == 0) {
sprintf_s(port, sizeof(port), "%s", pinfo[i].pPortName);
break;
}
}
free(pinfo);
gsapi_set_arg_encoding(gs_handle, GS_ARG_ENCODING_UTF8);
gsapi_set_stdio(gs_handle, NULL, NULL, NULL);
gsapi_init_with_args(gs_handle, argc, (char**)argv);
gsapi_set_arg_encoding(gs_handle, GS_ARG_ENCODING_UTF8);
gsapi_set_stdio(gs_handle, NULL, NULL, NULL);
gsapi_set_poll(gs_handle, NULL);
gsapi_set_display_callback(gs_handle, NULL);
gsapi_set_stdin(gs_handle, NULL);
gsapi_set_stdout(gs_handle, NULL);
// 打印PDF文件
wchar_t command[1024] = { 0 };
swprintf_s(command, sizeof(command), L"-dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=\"%%printer%s\" \"%s\"", port, filename);
char* argv2[64] = { 0 };
int argc2 = 0;
const wchar_t* p = command;
while (*p) {
argv2[argc2++] = (char*)p;
while (*p && !iswspace(*p))
p++;
if (*p)
*p++ = 0;
}
gsapi_exit(gs_handle);
gsapi_delete_instance(gs_handle);
}
int main()
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
wchar_t filename[] = L"C:\\test.pdf";
wchar_t printer_name[] = L"Microsoft Print to PDF";
print_pdf(filename, printer_name);
Gdiplus::GdiplusShutdown(gdiplusToken);
return 0;
}
```
需要注意的是,使用Ghostscript打印PDF文件需要确保Ghostscript已经正确安装并设置好环境变量。另外,在打印PDF文件前,需要先通过Ghostscript API初始化Ghostscript引擎,并设置输出打印机等参数。最后,调用Ghostscript API打印PDF文件即可。
c++在windows平台下使用ghostscript的api接口静默打印pdf
Ghostscript是一款开源的PostScript和PDF解释器,可以在Windows平台下使用其API接口进行静默打印PDF。
以下是实现步骤:
1. 下载并安装Ghostscript
从Ghostscript官网(https://www.ghostscript.com/download/gsdnld.html)下载Windows平台下的安装程序,安装到本地计算机上。
2. 编写代码
使用Ghostscript提供的API接口,在代码中实现PDF文件的静默打印。以下是一个示例代码:
```
#include <stdio.h>
#include "gsapi.h"
int main(int argc, char *argv[])
{
const char *pdfFile = "C:\\test.pdf";
const char *printerName = "My Printer";
const char *deviceName = "mswinpr2";
const char *outputFile = "C:\\test.prn";
const char *outputFormat = "PRN";
const char *args[] = {
"pdf2ps", "-sDEVICE=mswinpr2",
"-sOutputFile=C:\\test.prn",
"-dNOPAUSE", "-dBATCH", "-dSAFER",
"C:\\test.pdf", NULL
};
int argcCount = sizeof(args) / sizeof(args[0]) - 1;
gsapi_restart_ijs(NULL);
gsapi_revision_t revision;
gsapi_revision(&revision, sizeof(revision));
printf("Ghostscript version: %d.%d.%d\n", revision.product, revision.major, revision.minor);
gsapi_instance *instance;
int errorCode = gsapi_new_instance(&instance, NULL);
if (errorCode < 0) {
printf("Failed to create Ghostscript instance: %d\n", errorCode);
return -1;
}
gsapi_set_stdio(instance, stdin, stdout, stderr);
gsapi_set_poll(instance, NULL);
errorCode = gsapi_init_with_args(instance, argcCount, (char **)args);
if (errorCode < 0) {
printf("Failed to initialize Ghostscript instance: %d\n", errorCode);
return -1;
}
errorCode = gsapi_exit(instance);
if (errorCode < 0) {
printf("Failed to exit Ghostscript instance: %d\n", errorCode);
return -1;
}
// Send the output file to the printer
HANDLE printerHandle;
DOC_INFO_1 docInfo;
DWORD bytesWritten;
BOOL success;
success = OpenPrinter((LPSTR) printerName, &printerHandle, NULL);
if (!success) {
printf("Failed to open printer: %d\n", GetLastError());
return -1;
}
docInfo.pDocName = "Test Document";
docInfo.pOutputFile = NULL;
docInfo.pDatatype = "RAW";
success = StartDocPrinter(printerHandle, 1, (LPBYTE) &docInfo);
if (!success) {
printf("Failed to start print job: %d\n", GetLastError());
ClosePrinter(printerHandle);
return -1;
}
success = StartPagePrinter(printerHandle);
if (!success) {
printf("Failed to start page: %d\n", GetLastError());
EndDocPrinter(printerHandle);
ClosePrinter(printerHandle);
return -1;
}
HANDLE fileHandle = CreateFile(outputFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle == INVALID_HANDLE_VALUE) {
printf("Failed to open output file: %d\n", GetLastError());
EndPagePrinter(printerHandle);
EndDocPrinter(printerHandle);
ClosePrinter(printerHandle);
return -1;
}
BYTE buffer[4096];
DWORD bytesRead;
while (ReadFile(fileHandle, buffer, sizeof(buffer), &bytesRead, NULL)) {
if (bytesRead == 0) {
break;
}
success = WritePrinter(printerHandle, buffer, bytesRead, &bytesWritten);
if (!success) {
printf("Failed to write to printer: %d\n", GetLastError());
CloseHandle(fileHandle);
EndPagePrinter(printerHandle);
EndDocPrinter(printerHandle);
ClosePrinter(printerHandle);
return -1;
}
}
CloseHandle(fileHandle);
success = EndPagePrinter(printerHandle);
if (!success) {
printf("Failed to end page: %d\n", GetLastError());
EndDocPrinter(printerHandle);
ClosePrinter(printerHandle);
return -1;
}
success = EndDocPrinter(printerHandle);
if (!success) {
printf("Failed to end print job: %d\n", GetLastError());
ClosePrinter(printerHandle);
return -1;
}
ClosePrinter(printerHandle);
return 0;
}
```
3. 编译和运行
使用Visual Studio等编译器编译代码,生成可执行文件。在命令行中运行可执行文件,即可实现PDF文件的静默打印。
```
> ghostprint.exe
```
该示例代码将PDF文件转换为PRN格式的文件,并发送到指定的打印机进行打印。可以根据实际需求修改代码中的参数和变量。
阅读全文