Convert to PDF

Convert many of your office documents to PDF. For example, take your word document (.doc or docx) and call this API to convert it to PDF.

Offers conversion of common work documents to PDF. You can easily convert the following Non-PDF documents:

  • MS Word, Excel, PowerPoint
  • Open Office, Libre Office
  • Email (MSG, EML, MIME)
  • Plain Text (ASCII, UNICODE), WordPerfect
  • Image Formats: BMP, GIF, JPEG, PNG, TIFF, JB2, JP2, JPX, PBM

Documents with additional settings like Macros, merge fields or interactive forms may not be converted properly.

Depending on the type of filename, the source file type is recognized and converted to PDF.

To see how good the PDF result is, just copy and paste the curl sample and run from your command line.

FeatureParameterResponseActionDescriptionLinks
convertToPdfConvertToPdfConvertToPdfResConvertToPdfActionCreates textual or image stamps on PDF documents. Swagger
Sample
convertFileToPdftext,
pages,
alignX,alignY
file
file stream
Creates textual or image stamps on PDF documents.
Swagger
Sample

Samples

ConvertToPdf

  • curl
  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
curl No Sample
// setup convertToPdf object
var convertToPdf = new ConvertToPdf()
{
    // document
    Document = new Document()
    {
        DocData = File.ReadAllBytes("myWordDoc.docx"),
        Name = "myWordDoc.docx",
    },
    // action
    ConvertToPdfAction = new ConvertToPdfAction()
};

// conversion
var res = await Pdf4meClient.Pdf4me.Instance.ConvertClient.ConvertToPdfAsync(convertToPdf);

// extract the generated PDF and write it to disk
byte[] generatedPdf = res.Document.DocData;
File.WriteAllBytes("generatedPdf.pdf", generatedPdf);
// setup the convertClient
ConvertClient convertClient = new ConvertClient(pdf4meClient);

// setup convertToPdf object
ConvertToPdf convertToPdf = new ConvertToPdf();
// document
Document document = new Document();
document.setDocData(Files.readAllBytes(Paths.get("myWordDoc.docx")));
document.setName("myWordDoc.docx");
convertToPdf.setDocument(document);
// action
convertToPdf.setConvertToPdfAction(new ConvertToPdfAction());

// conversion
ConvertToPdfRes res = convertClient.convertToPdf(convertToPdf);

// extracting the generated PDF and writing it to disk
byte[] generatedPdf = res.getDocument().getDocData();
FileUtils.writeByteArrayToFile(new File("generatedPdf.pdf"), generatedPdf);
// create pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// create convertToPdf object
const convertToPdfReq = {
  // document
  document: {
    docData: fs.readFileSync(path.join(__dirname, 'myWordDoc.docx')).toString('base64'),
    name: 'myWordDoc.docx',
  },
  // action
  convertToPdfAction: {
    pdfConformance: 'pdfA1',
    conversionMode: 'fast',
  },
}

// conversion
pdf4meClient.convertToPdf(convertToPdfReq)
  .then(function(convertToPdfRes) {
    // extract the generated PDF and write it to disk
    const pdfDocument = Buffer.from(convertToPdfRes.document.docData, 'base64')
    fs.writeFileSync(path.join(__dirname, 'generatedPdf.pdf'), pdfDocument)
  })
  .catch(err => {
    console.log(err)
  })
// setup the convert_client
$client = new pdf4meAPI($token);

// conversion
$res = $client->pdf4me()->convertToPdf([
        // document
        "document"=> [
            "name"=> "myWordDoc.docx",
            "docData" => $client->getFileData('myWordDoc.docx')
        ],
        //action
        "convertAction"=>[]
    ]);

// extract the generated PDF and write it to disk
$generatedPdf = base64_decode($res->document->docData);
file_put_contents('generatedPdf.pdf', $generatedPdf);
# setup the convert_client
convert_client = ConvertClient(pdf4me_client)

# create the convert_to_pdf object
convert_to_pdf = ConvertToPdf(
    document=Document(
        doc_data=FileReader().get_file_data('myWordDoc.docx'),
        name='myWordDoc.docx'
    ),
    convert_to_pdf_action=ConvertToPdfAction()
)

# conversion
res = convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

# extracting the generated PDF
generated_pdf = base64.b64decode(res['document']['doc_data'])
# writing it to disk
with open('generatedPdf.pdf', 'wb') as f:
    f.write(generated_pdf)
file = '/path/to/file/myWordDoc.docx'

a = Pdf4me::ConvertToPdf.new(
    document: Pdf4me::Document.new(
      doc_data: Base64.encode64(File.open(file, 'rb', &:read)),
      name: "myWordDoc.docx"
    ),
    convert_to_pdf_action: Pdf4me::ConvertToPdfAction.new
  )
response = a.run

File.open('generatedPdf.pdf', 'wb') do |f|
       f.write(Base64.decode64(response.document.doc_data))
end

ConvertFileToPdf

  • curl
  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
curl https://api.pdf4me.com/Convert/ConvertFileToPdf ^
    -H "Authorization: Basic DEV-KEY" ^
    -F "file=@./myWordDoc.docx" ^
    -o ./generatedPdf.pdf
// conversion and writing the generated PDF to disk
byte[] generatedPdf = await Pdf4meClient.Pdf4me.Instance.ConvertClient.ConvertFileToPdfAsync(File.ReadAllBytes("myWordDoc.docx"), "myWordDoc.docx");
// and writing the generated PDF to disk
File.WriteAllBytes("generatedPdf.pdf", generatedPdf);
// setup the convertClient
ConvertClient convertClient = new ConvertClient(pdf4meClient);

// conversion and writing the generated PDF to disk
byte[] generatedPdf = convertClient.convertFileToPdf("myWordDoc.docx", new File("myWordDoc.docx"));
FileUtils.writeByteArrayToFile(new File("generatedPdf.pdf"), generatedPdf);
// create pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// conversion
pdf4meClient
  .convertFileToPdf(fs.createReadStream(path.join(__dirname, 'wordDoc.docx')))
  .then(function(pdfDocument) {
    // and writing the generated PDF to disk
    fs.writeFileSync(path.join(__dirname, 'convertFileToPdf_result.pdf'), pdfDocument)
  })
  .catch(err => {
    console.error(err)
  })
 
$res = $client->pdf4me()->convertFileToPdf(
    [
        "fName" => "myWordDoc.docx"
        "file" => __DIR__.'/myWordDoc.docx'
    ]
);

//writing it to file
file_put_contents('generatedPdf.pdf', $res);
# setup the convert_client
convert_client = ConvertClient(pdf4me_client)

# conversion
generated_pdf = convert_client.convert_file_to_pdf(
    file_name='myWordDoc.docx',
    file=FileReader().get_file_handler('myWordDoc.docx')
)
# writing the generated PDF to disk
with open('generatedPdf.pdf', 'wb') as f:
    f.write(generated_pdf)
b = Pdf4me::ConvertFileToPdf.new(
        file: '/path/to/file/myWordDoc.docx',
        save_path: 'generatedPdf.pdf'
     )
b.run # returns true|false

Models

ConvertToPdf

Name Type Description Notes
document Document
convertToPdfAction ConvertToPdfAction
jobId String [optional]
jobIdExtern String [optional]
integrations [String] [optional]

ConvertToPdfActionAction

Name Type Description Notes
pdfConformance String Compliance level of the generated PDF. [optional]
conversionMode String Conversion mode. [optional]

ConvertToPdfRes

Name Type Description Notes
document Document Generated PDF document.

Document

Name Type Description Notes
jobId String JobId of Documents WorkingSet.
documentId String Document Id
name String Filename inlcuding filetype.
docStatus String Status of the Document, e.g. Stamped.
pages Page Description of pages.
docData [byte] Document bytes.
docMetadata DocMetadata Document metadata such as title, pageCount et al.
docLogs DocLog Logging information about the request, e.g. timestamp.

Page

Name Type Description Notes
documentId String Globally unique Id.
pageId String Globally unique Id.
pageNumber Integer PageNumber, starting with 1.
rotate double By how much the page was rotated from its original orientation.
thumbnail byte Thumbnail representing this particular page.
sourceDocumentId String Id of the document it was created from, e.g. in case of an extraction, the result's sourceDocumentId is the Id of the PDF the pages have been extracted from.
sourcePageNumber Integer Page number of the original page in the original document, e.g. let's assume document B consists of page number 4 of document A (extraction).
Thus, document B's only page's sourcePageNumber is number 4.

DocMetadata

Name Type Description Notes
title String Title of document.
subject String Subject of document.
pageCount long Number of pages.
size long Number bytes of the document.
isEncrypted boolean If the document is Encrypted
pdfCompliance String Pdf Compliance, e.g. PDF/A.
isSigned boolean If the document is Encrypted
uploadedMimeType String Uploaded MimeType, e.g. application/bson.
uploadedFileSize long Uploaded file size.

DocLog

Name Type Description Notes
messageType String MessageType, e.g. PdfALog.
message String Message itself, e.g. a warning.
timestamp dateTime Timestamp.
docLogLevel String Type of message. Supported Values :
"verbose", "info", "warning", "error", "timing"
durationMilliseconds long Timing for requested log information [ms].

How can we help?