Page 1 of 1
class PdfDocument in Switch 25.11
Posted: Mon Jan 05, 2026 3:36 pm
by cotto
Hello and a happy new year!
I updated Switch from 2024 Fall to 25.11.
After that, Switch throws "ReferenceError: PdfDocument is not defined"
Updating the devDependencies from 'v24.0.2-final-updated.tar.gz' to 'v24.1.1-final.tar.gz' didn't solve the problem.
Is there anything I need to be aware of?
Thanks in advance!
Re: class PdfDocument in Switch 25.11
Posted: Tue Jan 06, 2026 2:54 pm
by freddyp
Modifying the typings file was a first and correct thing to do, but there is another thing.
The manifest.xml file in the script folder specifies the Switch version with which the script folder was created. When Switch runs a script it uses that information to choose which NodeJS version to use. What is probably happening here is that a NodeJS version is being used in which the PdfDocument class was not yet available.
The PdfDocument class was already available in 2024 Fall, but if my suspicion is correct, the script folder is older than that.
Re: class PdfDocument in Switch 25.11
Posted: Thu Jan 08, 2026 11:53 am
by cotto
I made the recommended changes, but the problem still occurs.
I then ran a few tests.
I use the switch types in my own modules, which are also imported into the main project.
When I comment out the methods from these modules that use the PdfDocument class, the script works without the changes.
Does this mean that typing in separate modules no longer works?
Or is a special configuration required?
Re: class PdfDocument in Switch 25.11
Posted: Fri Jan 09, 2026 9:29 am
by freddyp
There are quite some settings involved in the package.json, tsconfig.json and the code itself. It may be just one of the settings that is wrong, but there is only one way to tell and that is by looking at the whole picture. Please report this to support and provide the script folder and the package so we can investigate. We will report our findings here as well so other users can learn from it.
Re: class PdfDocument in Switch 25.11
Posted: Fri Jan 09, 2026 12:55 pm
by cotto
Thanks, I have done that.
Re: class PdfDocument in Switch 25.11
Posted: Wed Jan 14, 2026 12:57 pm
by samw
Providing an update on this after some initial investigation. It appears there is an issue in 25.11 which means that classes and enums provided by the Switch scripting module cannot be accessed directly from within a CommonJS module, however
ES modules are not affected.
This has been logged as a bug with internal ticket number
ENFS-38028 and we will address it in due course. For now the best workaround is either to use ES modules or to keep the use of provided classes and enums inside your main script file.
There is no issue with using the
switch-scripting type declarations themselves in a CJS module, since they are not used at runtime – all should work as expected when you are passing instances (e.g. of
Job,
FlowElement etc.) to the methods provided provided by your module.
The issue comes when you need to use a class or enum that we provide via the context of the script execution, such as
PdfDocument,
XmlDocument,
LogLevel,
AccessLevel etc. So you could pass an instance of
Job to a function in your module which calls
Job.log(LogLevel.Info, "Hello from my CJS module") but the error will be thrown by the
LogLevel.Info part (a workaround would simply be to pass
"info" as LogLevel).
Code: Select all
// To give more examples of things that will throw an error if used in a CJS module:
const pdfDoc = PdfDocument.open("path/to/file.pdf"); // ReferenceError: PdfDocument is not defined
const pageCount = PdfDocument.getNumberOfPages(); // ReferenceError: PdfDocument is not defined
const xmlDoc = XmlDocument.open() // ReferenceError: XmlDocument is not defined
const jobPath = await job.get(AccessLevel.ReadWrite); // ReferenceError: AccessLevel is not defined
await job.sendToData(Connection.Level.Warning); // ReferenceError: Connection is not defined
// And so on...