Skip to content

Commit b95ad96

Browse files
committed
Perform a check that there are printers installed. If there are not, then don't attempt to convert as office will complain.
Also checks the /printer and /fallback_printer names are installed
1 parent cebe321 commit b95ad96

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

OfficeToPDF/Program.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections;
2222
using System.Collections.Generic;
2323
using System.Diagnostics;
24+
using System.Drawing.Printing;
2425
using System.IO;
2526
using System.Reflection;
2627
using System.Security.AccessControl;
@@ -48,7 +49,8 @@ public enum ExitCode : int
4849
WorksheetNotFound = 256,
4950
EmptyWorksheet = 512,
5051
PDFProtectedDocument = 1024,
51-
ApplicationError = 2048
52+
ApplicationError = 2048,
53+
NoPrinters = 4096
5254
}
5355

5456
public enum MergeMode : int
@@ -135,6 +137,14 @@ static void Main(string[] args)
135137
options["pdf_restrict_accessibility_extraction"] = false;
136138
options["pdf_restrict_full_quality"] = false;
137139

140+
// We need some printers to keep office happy
141+
Dictionary<string,bool> installedPrinters = GetInstalledPrinters();
142+
if (installedPrinters.Count <= 0)
143+
{
144+
Console.WriteLine("There are no installed printers, so conversion can not proceed");
145+
Environment.Exit((int)(ExitCode.Failed | ExitCode.NoPrinters));
146+
}
147+
138148
// Strings used in error messages for different options
139149
var optionNameMap = new Dictionary<string, string>()
140150
{
@@ -391,11 +401,20 @@ static void Main(string[] args)
391401
case "printer":
392402
case "fallback_printer":
393403
// Only accept the next option if there are enough options
404+
string optname = itemMatch.Groups[1].Value.ToLower();
394405
if (argIdx + 2 < args.Length)
395406
{
396-
options[itemMatch.Groups[1].Value.ToLower()] = args[argIdx + 1];
407+
options[optname] = args[argIdx + 1];
397408
argIdx++;
398409
}
410+
if (optname.Equals("printer") || optname.Equals("fallback_printer"))
411+
{
412+
if (!installedPrinters.ContainsKey(((string)options[optname]).ToLowerInvariant())) {
413+
// The requested printer did not exists
414+
Console.WriteLine("The printer \"{0}\" is not installed", options[optname]);
415+
Environment.Exit((int)(ExitCode.Failed | ExitCode.InvalidArguments));
416+
}
417+
}
399418
break;
400419
case "screen":
401420
options["print"] = false;
@@ -988,6 +1007,16 @@ static PdfDocument ReadExistingPDFDocument(String filename, String generatedFile
9881007
return dstDoc;
9891008
}
9901009

1010+
private static Dictionary<string, bool> GetInstalledPrinters()
1011+
{
1012+
Dictionary<string, bool> printers = new Dictionary<string, bool>();
1013+
foreach (string name in PrinterSettings.InstalledPrinters)
1014+
{
1015+
printers[name.ToLowerInvariant()] = true;
1016+
}
1017+
return printers;
1018+
}
1019+
9911020
static void ShowHelp()
9921021
{
9931022
Console.Write(@"Converts Office documents to PDF from the command line.

OfficeToPDF/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.8.20.0")]
35-
[assembly: AssemblyFileVersion("1.8.20.0")]
34+
[assembly: AssemblyVersion("1.8.21.0")]
35+
[assembly: AssemblyFileVersion("1.8.21.0")]

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,13 @@ The following optional switches can be used:
9292
| /word_header_dist _pts_ | the distance (in points) from the header to the top of the page |
9393
| /word_footer_dist _pts_ | the distance (in points) from the footer to the bottom of the page |
9494
| /word_field_quick_update | perform a fast update of fields in Word before conversion |
95+
| /word_fix_table_columns | update table column widths to match table heading column widths |
9596
| /word_keep_history | do not clear Word's recent files list |
9697
| /word_max_pages _pages_ | do not attempt conversion of a Word document if it has more than this number of _pages_ |
9798
| /word_no_field_update | do not update fields when creating the PDF |
9899
| /word_ref_fonts | when fonts are not available, a reference to the font is used in the generated PDF rather than a bitmapped version. The default is for a bitmap of the text to be used |
100+
| /fallback_printer <name> | print the document to postscript printer <name> for conversion when the main conversion routine fails. Requires Ghostscript to be installed |
101+
| /printer <name> | print the document to postscript printer <name> for conversion. Requires Ghostscript to be installed |
99102
| /pdf_clean_meta _type_ | allows for some meta-data to be removed from the generated PDF<br>_type_ can be:<ul><li>basic - removes author, keywords, creator and subject</li><li>full - removes all that basic removes and also the title</li></ul> |
100103
| /pdf_layout _layout_ | controls how the pages layout in Acrobat Reader<br>_layout_ can be one of the following values:<ul><li>onecol - show pages as a single scrolling column</li><li>single - show pages one at a time</li><li>twocolleft - show pages in two columns, with odd-numbered pages on the left</li><li>twocolright - show pages in two columns, with odd-numbered pages on the right</li><li>twopageleft - show pages two at a time, with odd-numbered pages on the left</li><li>twopageright - show pages two at a time, with odd-numbered pages on the right</li></ul> |
101104
| /pdf_page_mode _mode_ | controls how the PDF will open with Acrobat Reader<br>_mode_ can be one of the following values:<ul><li>full - the PDF will open in fullscreen mode</li><li>bookmarks - the PDF will open with the bookmarks visible</li><li>thumbs - the PDF will open with the thumbnail view visible</li><li>none - the PDF will open without the navigation bar visible</li></ul> |
@@ -135,6 +138,8 @@ so bitwise operations can test for multiple errors.
135138
256 - The requested worksheet was not found
136139
512 - Unable to use an empty worksheet
137140
1024 - Unable to modify or open a protected PDF
141+
2048 - Raised when there is a problem calling an Office application
142+
4096 - There are no printers installed, so Office conversion can not proceed
138143
```
139144

140145
---

0 commit comments

Comments
 (0)