static BaseEngine *psgz2pdf(const WCHAR *fileName) { ScopedMem<WCHAR> tmpFile(path::GetTempPath(L"PsE")); ScopedFile tmpFileScope(tmpFile); if (!tmpFile) return NULL; gzFile inFile = gzopen_w(fileName, "rb"); if (!inFile) return NULL; FILE *outFile = NULL; errno_t err = _wfopen_s(&outFile, tmpFile, L"wb"); if (err != 0 || !outFile) { gzclose(inFile); return NULL; } char buffer[12*1024]; for (;;) { int len = gzread(inFile, buffer, sizeof(buffer)); if (len <= 0) break; fwrite(buffer, 1, len, outFile); } fclose(outFile); gzclose(inFile); return ps2pdf(tmpFile); }
static BaseEngine *ps2pdf(const WCHAR *fileName) { // TODO: read from gswin32c's stdout instead of using a TEMP file ScopedMem<WCHAR> shortPath(path::ShortPath(fileName)); ScopedMem<WCHAR> tmpFile(path::GetTempPath(L"PsE")); ScopedFile tmpFileScope(tmpFile); ScopedMem<WCHAR> gswin32c(GetGhostscriptPath()); if (!shortPath || !tmpFile || !gswin32c) return NULL; // try to help Ghostscript determine the intended page size ScopedMem<WCHAR> psSetup; RectI page = ExtractDSCPageSize(fileName); if (!page.IsEmpty()) psSetup.Set(str::Format(L" << /PageSize [%i %i] >> setpagedevice", page.dx, page.dy)); ScopedMem<WCHAR> cmdLine(str::Format( L"\"%s\" -q -dSAFER -dNOPAUSE -dBATCH -dEPSCrop -sOutputFile=\"%s\" -sDEVICE=pdfwrite -c \".setpdfwrite%s\" -f \"%s\"", gswin32c, tmpFile, psSetup ? psSetup : L"", shortPath)); fprintf(stderr, "- %s:%d: using '%ls' for creating '%%TEMP%%\\%ls'\n", path::GetBaseName(__FILE__), __LINE__, gswin32c.Get(), path::GetBaseName(tmpFile)); // TODO: the PS-to-PDF conversion can hang the UI for several seconds HANDLE process = LaunchProcess(cmdLine, NULL, CREATE_NO_WINDOW); if (!process) return NULL; DWORD timeout = 10000; #ifdef DEBUG // allow to disable the timeout for debugging purposes if (GetEnvironmentVariable(L"SUMATRAPDF_NO_GHOSTSCRIPT_TIMEOUT", NULL, 0)) timeout = INFINITE; #endif DWORD exitCode = EXIT_FAILURE; WaitForSingleObject(process, timeout); GetExitCodeProcess(process, &exitCode); TerminateProcess(process, 1); CloseHandle(process); if (exitCode != EXIT_SUCCESS) return NULL; size_t len; ScopedMem<char> pdfData(file::ReadAll(tmpFile, &len)); if (!pdfData) return NULL; ScopedComPtr<IStream> stream(CreateStreamFromData(pdfData, len)); if (!stream) return NULL; return PdfEngine::CreateFromStream(stream); }
static PdfEngine *ps2pdf(const WCHAR *fileName) { // TODO: read from gswin32c's stdout instead of using a TEMP file ScopedMem<WCHAR> shortPath(path::ShortPath(fileName)); ScopedMem<WCHAR> tmpFile(path::GetTempPath(L"PsE")); ScopedFile tmpFileScope(tmpFile); ScopedMem<WCHAR> gswin32c(GetGhostscriptPath()); if (!shortPath || !tmpFile || !gswin32c) return NULL; ScopedMem<WCHAR> cmdLine(str::Format(L"\"%s\" -q -dSAFER -dNOPAUSE -dBATCH -dEPSCrop -sOutputFile=\"%s\" -sDEVICE=pdfwrite -c .setpdfwrite -f \"%s\"", gswin32c, tmpFile, shortPath)); if (GetEnvironmentVariable(L"MULOG", NULL, 0)) { wprintf(L"ps2pdf: using Ghostscript from '%s'\n", gswin32c.Get()); wprintf(L"ps2pdf: for creating '%s'\n", tmpFile.Get()); } // TODO: the PS-to-PDF conversion can hang the UI for several seconds HANDLE process = LaunchProcess(cmdLine, NULL, CREATE_NO_WINDOW); if (!process) return NULL; DWORD exitCode = EXIT_FAILURE; WaitForSingleObject(process, 10000); GetExitCodeProcess(process, &exitCode); TerminateProcess(process, 1); CloseHandle(process); if (exitCode != EXIT_SUCCESS) return NULL; size_t len; ScopedMem<char> pdfData(file::ReadAll(tmpFile, &len)); if (!pdfData) return NULL; ScopedComPtr<IStream> stream(CreateStreamFromData(pdfData, len)); if (!stream) return NULL; if (GetEnvironmentVariable(L"MULOG", NULL, 0)) printf("ps2pdf: PDF conversion successful\n"); return PdfEngine::CreateFromStream(stream); }