CreatePDFOperation

CreatePDFOperation

An operation that converts a non-PDF file to a PDF file. Some source formats may have associated conversion parameters which can be set in the CreatePDFOperation#setOptions method.

The supported source media types are listed here. The SupportedSourceFormat object can be used to map file extensions to their corresponding media types when creating FileRef instances for the source files.

  • application/msword
  • application/vnd.ms-excel
  • application/vnd.ms-powerpoint
  • application/vnd.openxmlformats-officedocument.presentationml.presentation
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • application/zip (see "Special handling for HTML inputs" below)
  • image/bmp
  • image/gif
  • image/jpeg
  • image/png
  • image/tiff
  • text/plain
  • text/rtf

Special handling for HTML inputs:

An HTML input can only be provided as a local zip archive. When creating the corresponding FileRef instance, the media type must be "application/zip" for the local zip archive. Zip archive must have the following structure:

  • The main HTML file must be named "index.html".
  • "index.html" must exist at the top level of zip archive, not in a folder.

Sample layout:

   html_files.zip
   |__index.html
   |__referenced_file_1.css
   |__referenced_file_2.jpeg
   |__subfolder_1
   |_____referenced_file_3.jpeg
 

Sample Usage:


 const credentials = PDFToolsSdk.Credentials.serviceAccountCredentialsBuilder()
           .fromFile("pdftools-api-credentials.json")
           .build(),
       executionContext = PDFToolsSdk.ExecutionContext.create(credentials),
       CreatePDF = PDFToolsSdk.CreatePDF,
       createPDFOperation = CreatePDF.Operation.createNew(),
       input = PDFToolsSdk.FileRef.createFromLocalFile('test/resources/createPDFInput.docx', CreatePDF.SupportedSourceFormat.docx);

 createPDFOperation.setInput(input);

 createPDFOperation.execute(executionContext)
     .then(result => result.saveAsFile('output/createPDF.pdf'))
     .catch(err => console.log(err));

Members

(static, constant) SupportedMediaTypes

Deprecated:
  • Since version 1.1.0; Use SupportedSourceFormat instead.
Properties:
Name Type Description
bmp string

Represents "image/bmp" media type

doc string

Represents "application/msword" media type

docx string

Represents "application/vnd.openxmlformats-officedocument.wordprocessingml.document" media type

gif string

Represents "image/gif" media type

jpeg string

Represents "image/jpeg" media type

jpg string

Represents "image/jpeg" media type

png string

Represents "image/png" media type

ppt string

Represents "application/vnd.ms-powerpoint" media type

pptx string

Represents "application/vnd.openxmlformats-officedocument.presentationml.presentation" media type

rtf string

Represents "text/rtf" media type

tif string

Represents "image/tiff" media type

tiff string

Represents "image/tiff" media type

txt string

Represents "text/plain" media type

xls string

Represents "application/vnd.ms-excel" media type

xlsx string

Represents "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" media type

zip string

Represents "application/zip" media type

Supported source file formats for CreatePDFOperation.

(static, constant) SupportedSourceFormat

Properties:
Name Type Description
bmp string

Represents "image/bmp" media type

doc string

Represents "application/msword" media type

docx string

Represents "application/vnd.openxmlformats-officedocument.wordprocessingml.document" media type

gif string

Represents "image/gif" media type

jpeg string

Represents "image/jpeg" media type

jpg string

Represents "image/jpeg" media type

png string

Represents "image/png" media type

ppt string

Represents "application/vnd.ms-powerpoint" media type

pptx string

Represents "application/vnd.openxmlformats-officedocument.presentationml.presentation" media type

rtf string

Represents "text/rtf" media type

tif string

Represents "image/tiff" media type

tiff string

Represents "image/tiff" media type

txt string

Represents "text/plain" media type

xls string

Represents "application/vnd.ms-excel" media type

xlsx string

Represents "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" media type

zip string

Represents "application/zip" media type

Supported source file formats for CreatePDFOperation.

Methods

(static) createNew() → {CreatePDFOperation}

Constructs a CreatePDFOperation instance.

Returns:

A new CreatePDFOperation instance.

Type
CreatePDFOperation

setInput(sourceFileRefnon-null)

Sets an input file.

Parameters:
Name Type Description
sourceFileRef FileRef

An input file.

setOptions(options)

Sets the conversion parameters for this operation.

Parameters:
Name Type Description
options Object

Conversion parameters; use null for default values. The type of this parameter depends on the source format of the file which is being converted to PDF. For example, for HTML to PDF conversions, the type will be CreatePDFFromHtmlOptions. For other supported options, refer the options key. in module:PDFToolsSDK#CreatePDF.

execute(contextnon-null) → {Promise.<FileRef>}

Executes this operation using the supplied context and returns a Promise which resolves to the operation result.

The resulting file may be stored in the system temporary directory (per the os.tempdir(), symlinks are resolved to the actual path). See FileRef for how temporary resources are cleaned up.

Parameters:
Name Type Description
context ExecutionContext

The context in which the operation will be executed.

Throws:
  • if an API call results in an error response.

    Type
    ServiceApiError
  • if service usage limits have been reached or credentials quota has been exhausted.

    Type
    ServiceUsageError
Returns:

A promise which resolves to the operation result.

Type
Promise.<FileRef>