Using 'pdftron.PDF.Convert.ToPdf()' in a ASP.NET to convert Word to PDF/PNG/Silverlight

Q: We are trying to use 'pdftron.PDF.Convert.ToPdf()' and
ToSilverlight() to convert MS Office documents (mainly Word) to PDF
and Silverlight XPS.

It works great in the console application but when we move the code to
our ASP.NET application (Windows 2008 x64 Web edition SP 2) we run
into the followng issue:

pdftron.Common.PDFNetException: Exception:
      Message: An error occurred while converting the file.
      Unable to find printer
   at pdftron.PDF.Convert.ToPdf(PDFDoc in_pdfdoc, String in_filename)

All other operations in PDFNet work without errors or exceptions.
--------------------------------

A: Although it is not related to your issue/exception the following
note applies when using 64-bit version on PDFNet on a 64-bit server:

// Note that when using the 64-bit version of PDFNet, it must be run
in a 64-bit web server.
// The Microsoft development web server is 32-bit only. The options
are to use the 32-bit version of PDFNet
// for development purposes, or to configure Visual Studio to run an
alternate, 64-bit web server, such as IIS.

Going back to the issue:

'pdftron.PDF.Convert.ToPdf()/ToXps()/ToSilverlight()' will fail to
conver MS Office documents if the application pool's identity is set
to "NETWORK SERVICE" or lower. This is because "NETWORK SERVICE"
identity has a very limited set of privileges (e.g. no access to
registry, no full access to file system, etc).

When 'Convert.ToPdf' is ran under "NETWORK SERVICE", converting
Microsoft Office documents fails because MS Interop itself does not
work under "NETWORK SERVICE". This will cause the Convert.ToPdf()
method to revert to the PDFNet printer. Using the PDFNet printer also
requires higher permission level in order to read and write entries to
the registry which is not allowed under "NETWORK SERVICE", so this
method will fail as well.

In order to resolve the issue, it is necessary to grant the required
permissions. Using the identity "LOCAL SYSTEM" works, but other
solutions are possible.

The solutions that we tried on our end require an actual user account
(possibly only with permissions needed by PDFNet). The "limited" user
account can be used as the IIS application pool identity instead of
"NETWORK SERVICE".

The other solution is to associate ASP.NET application to use
"limited" user's credentials via "ASP.NET Impersonation":
  http://msdn.microsoft.com/en-us/library/xh507fc5(v=VS.100).aspx

As mentioned by that article, adding the following code to web.config:

<configuration>
  <system.web>
    <identity impersonate="true" userName="computer\LimitedUser"
password="********" />
  </system.web>
</configuration>

will use this "limited" user account's credentials instead of "NETWORK
SERVICE" for this specific application.

Q: What are my options if I do not want to use MS Office in me
ASP.Net? That is I would like to use PDFNet printer driver because I
also need to convert from a format that is not supported by MS Office.
--------

A:Because issuing the print command (in pdftron.PDF.Convert.ToPdf)
spawns a new process, according to (http://support.microsoft.com/kb/
889251/en-us) it will run under the context of the ASP.NET worker
process:

"...in ASP.NET, impersonation is performed at the thread level and not
at the process level. Therefore, any process that you spawn from
ASP.NET will run under the context of the ASP.NET worker process and
not under the impersonated context."

This means that "ASP.NET Impersonation" (
http://msdn.microsoft.com/en-us/library/xh507fc5(v=VS.100).aspx )
will work if you want to convert files via MS Office interop
(pdftron.Convert.To...), but it will not work when converting the file
through the virtual printer driver. So if you are using the "NETWORK
SERVICE" account for ASP.NET application pool and Office is not
installed, pdftron.PDF.Convert.To...() will eventually time out and
fail.

There are a several ways to solve this:

1. "LOCAL SYSTEM" instead of "NETWORK SERVICE" ASP.NET application
pool. This case guarantees that the conversion process works (both
printer and Interop), but it has loose security.

2. Use the Microsoft Office Interop instead or PDFNet printer. This is
an option given that you are converting from MS Office formats (or
PDF, XPS, Silverlight, EMF, PNG, JPEG, TIFF, BMP, and GIF) and that
proper identity impersonation is set.

3. Perhaps the best solution is to create a separate console app that
will convert MS Office to PDF/Silverlight and then call this
application via a Shell command. In this case, it is possible to
provide Convert.To...() all the permissions it requires and still have
a secure ASP.NET application.

On Mar 2, 2:34 pm, Support <supp...@pdftron.com> wrote:

Q: We are trying to use 'pdftron.PDF.Convert.ToPdf()' and
ToSilverlight() to convert MS Office documents (mainly Word) to PDF
and Silverlight XPS.

It works great in the console application but when we move the code to
our ASP.NET application (Windows 2008 x64 Web edition SP 2) we run
into the followng issue:

pdftron.Common.PDFNetException: Exception:
Message: An error occurred while converting the file.
Unable to find printer
at pdftron.PDF.Convert.ToPdf(PDFDoc in_pdfdoc, String in_filename)

All other operations in PDFNet work without errors or exceptions.

--------------------------------

A: Although it is not related to your issue/exception the following
note applies when using 64-bit version on PDFNet on a 64-bit server:

// Note that when using the 64-bit version of PDFNet, it must be run
in a 64-bit web server.
// The Microsoft development web server is 32-bit only. The options
are to use the 32-bit version of PDFNet
// for development purposes, or to configure Visual Studio to run an
alternate, 64-bit web server, such as IIS.

Going back to the issue:

'pdftron.PDF.Convert.ToPdf()/ToXps()/ToSilverlight()' will fail to
conver MS Office documents if the application pool's identity is set
to "NETWORK SERVICE" or lower. This is because "NETWORK SERVICE"
identity has a very limited set of privileges (e.g. no access to
registry, no full access to file system, etc).

When 'Convert.ToPdf' is ran under "NETWORK SERVICE", converting
Microsoft Office documents fails because MS Interop itself does not
work under "NETWORK SERVICE". This will cause the Convert.ToPdf()
method to revert to the PDFNet printer. Using the PDFNet printer also
requires higher permission level in order to read and write entries to
the registry which is not allowed under "NETWORK SERVICE", so this
method will fail as well.

In order to resolve the issue, it is necessary to grant the required
permissions. Using the identity "LOCAL SYSTEM" works, but other
solutions are possible.

The solutions that we tried on our end require an actual user account
(possibly only with permissions needed by PDFNet). The "limited" user
account can be used as the IIS application pool identity instead of
"NETWORK SERVICE".

The other solution is to associate ASP.NET application to use
"limited" user's credentials via "ASP.NET Impersonation":
http://msdn.microsoft.com/en-us/library/xh507fc5(v=VS.100).aspx

As mentioned by that article, adding the following code to web.config:

<configuration>
<system.web>
<identity impersonate="true" userName="computer\LimitedUser"
password="********" />
</system.web>
</configuration>

will use this "limited" user account's credentials instead of "NETWORK
SERVICE" for this specific application.