Hello,
I am using HTMLPrint for an invoice printing solution which is in development. My company will shortly be purchasing a server license from you..
I would like to know, does htmlprint.exe have exit codes which indicate error conditions? If so, it would be helpful to know what the exit codes are and what error conditions they represent..
I am calling htmprint.exe from a C# application...
var startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = "htmlprint.exe",
Arguments = "{my html doc} -hidewindow"
};
var process = Process.Start(startInfo);
process.WaitForExit();
switch (process.ExitCode)
{
case -1: // html print error?
case 0: // html print success?
// etc...
}
I would be very grateful if you could clarify this point for me ?
With kind regards,
===============================================
Thakns for your message, please refer to return value of htmlprint.exe at below,
0: Printing successful
-10: Can't load necessary DLL files properly
-101: Can't start spooler service
-10: Can't install virtual printer correctly
-11: Something is wrong in command line options
-12: Can't load device settings from target printer
-19: Wrong value in "Copies"
5: Trial version is expired
-21: Can't change printer settings
VeryDOC
===============================================
Thank you, the list was very helpful to me!
I have one further question – is it possible to give the input to htmlprint.exe via stdin instead of an HTML file or URL?
In my application I have HTML stored in memory as a string. Right now I have to create a temporary file on disk to print...
string myHtml = “ etc etc... ”;
var tempFile = File.WriteAllText(“temp.htm”, myHtml);
Process.Start(“htmlPrint.exe temp.htm”).WaitForExit();
File.Delete(tempFile);
I would prefer to pass the string to stdin like this:
var startInfo = new ProcessStartInfo(“htmlprint.exe”)
startInfo.RedirectStandardInput = true;
var process = Process.Start(startInfo);
process.StandardInput.WriteLine(myHtml);
process.WaitForExit();
And then no html file is necessary - Is it possible to do?
===============================================
Thanks for your message, yes, read HTML content from “stdin” is a great solution, but the current version of HTMLPrint command line doesn’t support “stdin” yet, we will try to implement this function in the future, we will let you know after this function is available.
VeryDOC
===============================================
Hello VeryDOC,
I am using HTML Print command line utility happily at my company. I would like to share with you some helpful code I have written, it is used to execute htmlprint.exe.
This code is written in C# for using htmlprint in .NET applications – I hereby give you permission to share this code with your customers and/or offer it for download on your website. I hope it will benefit other users of htmlprint.exe ?
The license is open-source for anybody to use or modify.
With kind regards,
==================================
Thanks for your great information, I have attached the source code of HTML Print Command Line calling example at below, thank you again.
VeryDOC
==================================
#region License
// Microsoft Public License (MS-PL)
// This license governs use of the accompanying software. If you use the software, you
// accept this license. If you do not accept the license, do not use the software.
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the
// same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
// 3. Conditions and Limitations
// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#endregion
namespace VeryDoc.HtmlPrint
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
/// <summary>
/// Defines a helper class to build VeryDOC htmlprint command lines.
/// </summary>
/// <example>
/// <code lang="C#">
/// <![CDATA[
/// var startInfo = HtmlPrintCommandLineBuilder.BeginBuild()
/// .WithBottomMargin(25)
/// .WithCopies(1)
/// .WithHtmlFilePath("myHtmlFile.htm")
/// .WithLeftMargin(25)
/// .WithPrinter("myPrinter")
/// .WithRightMargin(25)
/// .WithTopMargin(25)
/// .WithRegistrationKey("XXXXXXXXXXXXXXXXXXX")
/// .ToProcessStartInfo("htmlprint.exe");
///
/// var htmlPrintProcess = System.Diagnostics.Process.Start(startInfo);
/// ]]>
/// </code>
/// </example>
public class HtmlPrintCommandLineBuilder
{
/// <summary>
/// Prevents a default instance of the <see cref="HtmlPrintCommandLineBuilder"/> class from
/// being created.
/// </summary>
private HtmlPrintCommandLineBuilder()
{
this.CommandLineSwitches = new List<String>();
}
/// <summary>
/// Gets or sets the list of command line switches which have been configured for this
/// build context.
/// </summary>
private List<String> CommandLineSwitches { get; set; }
/// <summary>
/// Gets or sets the path to the html file which should be printed.
/// </summary>
private String HtmlFilePath { get; set; }
/// <summary>
/// Begins the build context.
/// </summary>
/// <returns>A new <see cref="HtmlPrintCommandLineBuilder"/> instance.</returns>
static public HtmlPrintCommandLineBuilder BeginBuild()
{
return new HtmlPrintCommandLineBuilder();
}
/// <summary>
/// Defines a named printer for the build context.
/// </summary>
/// <param name="printerName">The name of the printer to use.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref name="printerName"/> parameter is null.
/// </exception>
public HtmlPrintCommandLineBuilder WithPrinter(String printerName)
{
if (String.IsNullOrEmpty(printerName))
throw new ArgumentNullException("printerName");
this.CommandLineSwitches.Add(String.Format("-printer \"{0}\"", printerName));
return this;
}
/// <summary>
/// Defines the number of copies to print.
/// </summary>
/// <param name="copies">The number of copies to print.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// Thrown if the value passed to the <paramref name="copies"/> parameter is less than one.
/// </exception>
public HtmlPrintCommandLineBuilder WithCopies(Int32 copies)
{
if (copies < 1)
throw new ArgumentOutOfRangeException("copies");
this.CommandLineSwitches.Add(String.Format("-copies {0}", copies));
return this;
}
/// <summary>
/// Defines the left margin when printing, measured in points.
/// </summary>
/// <param name="points">The margin size in points.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// Thrown if the value passed to the <paramref name="points"/> parameter is less than zero.
/// </exception>
public HtmlPrintCommandLineBuilder WithLeftMargin(Int32 points)
{
if (points < 0)
throw new ArgumentOutOfRangeException("points");
this.CommandLineSwitches.Add(String.Format("-marginleft {0}", points));
return this;
}
/// <summary>
/// Defines the top margin when printing, measured in points.
/// </summary>
/// <param name="points">The margin size in points.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// Thrown if the value passed to the <paramref name="points"/> parameter is less than zero.
/// </exception>
public HtmlPrintCommandLineBuilder WithTopMargin(Int32 points)
{
if (points < 0)
throw new ArgumentOutOfRangeException("points");
this.CommandLineSwitches.Add(String.Format("-margintop {0}", points));
return this;
}
/// <summary>
/// Defines the right margin when printing, measured in points.
/// </summary>
/// <param name="points">The margin size in points.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// Thrown if the value passed to the <paramref name="points"/> parameter is less than zero.
/// </exception>
public HtmlPrintCommandLineBuilder WithRightMargin(Int32 points)
{
if (points < 0)
throw new ArgumentOutOfRangeException("points");
this.CommandLineSwitches.Add(String.Format("-marginright {0}", points));
return this;
}
/// <summary>
/// Defines the bottom margin when printing, measured in points.
/// </summary>
/// <param name="points">The margin size in points.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// Thrown if the value passed to the <paramref name="points"/> parameter is less than zero.
/// </exception>
public HtmlPrintCommandLineBuilder WithBottomMargin(Int32 points)
{
if (points < 0)
throw new ArgumentOutOfRangeException("points");
this.CommandLineSwitches.Add(String.Format("-marginbottom {0}", points));
return this;
}
/// <summary>
/// Defines the header text to print.
/// </summary>
/// <param name="headerText">The header text.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref name="headerText"/> parameter is null or
/// empty.
/// </exception>
public HtmlPrintCommandLineBuilder WithHeader(String headerText)
{
if (String.IsNullOrEmpty(headerText))
throw new ArgumentNullException("headerText");
this.CommandLineSwitches.Add(String.Format("-header \"{0}\"", headerText));
return this;
}
/// <summary>
/// Defines the footer text to print.
/// </summary>
/// <param name="footerText">The footer text.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref name="footerText"/> parameter is null or
/// empty.
/// </exception>
public HtmlPrintCommandLineBuilder WithFooter(String footerText)
{
if (String.IsNullOrEmpty(footerText))
throw new ArgumentNullException("footerText");
this.CommandLineSwitches.Add(String.Format("-footer \"{0}\"", footerText));
return this;
}
/// <summary>
/// Instruct htmlprint.exe to hide the preview dialog.
/// </summary>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
public HtmlPrintCommandLineBuilder HideWindow()
{
this.CommandLineSwitches.Add("-hidewindow");
return this;
}
/// <summary>
/// Defines the registration key to pass to htmlprint.exe
/// </summary>
/// <param name="registrationKey">The registration key.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref name="registrationKey"/> parameter is null
/// or empty.
/// </exception>
public HtmlPrintCommandLineBuilder WithRegistrationKey(String registrationKey)
{
if (String.IsNullOrEmpty(registrationKey))
throw new ArgumentNullException("registrationKey");
this.CommandLineSwitches.Add(String.Format("-$ {0}", registrationKey));
return this;
}
/// <summary>
/// Sets the file path of the Html file which should be printed.
/// </summary>
/// <param name="htmlFilePath">The path of the Html file.</param>
/// <returns>The command line builder for continuation of the fluent build process.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref name="htmlFilePath"/> parameter is null or
/// empty.
/// </exception>
public HtmlPrintCommandLineBuilder WithHtmlFilePath(String htmlFilePath)
{
if (String.IsNullOrEmpty(htmlFilePath))
throw new ArgumentNullException("htmlFilePath");
this.HtmlFilePath = htmlFilePath;
return this;
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override String ToString()
{
return String.Join(" ", this.CommandLineSwitches.ToArray()) + " \"" + this.HtmlFilePath + "\"";
}
/// <summary>
/// Constructs a new <see cref="T:System.Diagnostics.ProcessStartInfo"/> object to invoke
/// htmlprint.exe with the configured command line arguments.
/// </summary>
/// <returns>A new <see cref="T:System.Diagnostics.ProcessStartInfo"/> object.</returns>
public ProcessStartInfo ToProcessStartInfo()
{
return this.ToProcessStartInfo("htmlprint.exe");
}
/// <summary>
/// Constructs a new <see cref="T:System.Diagnostics.ProcessStartInfo"/> object to invoke
/// htmlprint.exe with the configured command line arguments.
/// </summary>
/// <param name="exeFilePath">The file path to htmlprint.exe</param>
/// <returns>A new <see cref="T:System.Diagnostics.ProcessStartInfo"/> object.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// Thrown if the value passed to the <paramref name="exeFilePath"/> parameter is null or
/// empty.
/// </exception>
public ProcessStartInfo ToProcessStartInfo(String exeFilePath)
{
if (String.IsNullOrEmpty(exeFilePath))
throw new ArgumentNullException("exeFilePath");
return new ProcessStartInfo
{
FileName = exeFilePath,
Arguments = this.ToString()
};
}
}
}
=============================================
#region License
// Microsoft Public License (MS-PL)
// This license governs use of the accompanying software. If you use the software, you
// accept this license. If you do not accept the license, do not use the software.
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the
// same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
// 3. Conditions and Limitations
// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#endregion
namespace VeryDoc.HtmlPrint
{
using System;
/// <summary>
/// Defines an exception raised from the htmlprint.exe external tool.
/// </summary>
public class HtmlPrintException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="HtmlPrintException"/> class.
/// </summary>
public HtmlPrintException()
: base() { }
/// <summary>
/// Initializes a new instance of the <see cref="HtmlPrintException"/> class.
/// </summary>
/// <param name="message">The error message.</param>
public HtmlPrintException(String message)
: base(message) { }
/// <summary>
/// Initializes a new instance of the <see cref="HtmlPrintException"/> class.
/// </summary>
/// <param name="message">The error message.</param>
/// <param name="innerException">The inner exception.</param>
public HtmlPrintException(String message, Exception innerException)
: base(message, innerException) { }
/// <summary>
/// Creates a new <see cref="HtmlPrintException"/> object related to the specified exit
/// code.
/// </summary>
/// <param name="exitCode">The htmlprint.exe process exit code.</param>
/// <returns>A new <see cref="HtmlPrintException"/> object related to the specified exit
/// code, or null if the exit code is zero or indicates a non-error condition.</returns>
static public HtmlPrintException FromExitCode(Int32 exitCode)
{
switch (exitCode)
{
case -101:
{
return new HtmlPrintException("htmlprint.exe unable to start spooler service");
}
case -100:
{
return new HtmlPrintException("htmlprint.exe unable to load dependency dll");
}
case -21:
{
return new HtmlPrintException("htmlprint.exe unable to change printer settings");
}
case -19:
{
return new HtmlPrintException("htmlprint.exe invalid copies parameter");
}
case -18:
{
return new HtmlPrintException("htmlprint.exe unable to find specified printer");
}
case -12:
{
return new HtmlPrintException("htmlprint.exe cannot load device settings from target printer");
}
case -11:
{
return new HtmlPrintException("htmlprint.exe malformed command line");
}
case -10:
{
throw new HtmlPrintException("htmlprint.exe unable to install virtual printer");
}
case 0:
{
return null;
}
case 5:
{
return new HtmlPrintException("htmlprint.exe trial expired");
}
default:
{
if (exitCode < 0)
throw new HtmlPrintException("htmlprint.exe unknown error");
return null;
}
}
}
}
}