Cloudinary offers robust support for uploading, managing, transforming, and delivering PDF files, along with third-party integrations for converting documents and extracting text.
Uploading PDF Files
You can upload PDF files using any of the options available for uploading files to your Cloudinary account. To support transformations on your PDF files, these are uploaded as an `image` resource type by default.
Uploading a password-protected PDF file as an `image` resource type is not supported. If necessary, you can upload a password-protected PDF by setting the resource_type to `raw` in the upload command.
Delivering PDF Files
PDFs in Cloudinary can be delivered using standard URLs, with the option to convert them into image formats by changing the file extension. This will deliver the first page of the PDF by default, but you can use the `page` parameter (`pg` in the URL) to select a different page.
Advanced transformations, like cropping or adding overlays, can also be applied.
For example, cropping out a specific 300×300 square from the second page, with rounded corners and a black background:
Downloading PDF Files
For Free accounts: If you have uploaded a PDF file and want to share the URL for download, there is a security flag that needs to be changed before you will be able to make your PDF downloadable. Please refer to this article to correctly configure your account.
Creating PDF Files from Images:
Cloudinary provides the multi method allows you to merge images tagged with the same tag into a single multi-page PDF. The newly created PDF includes each image as a separate page, all ordered alphanumerically by their public ID. If you want the images to be included in a specific order, make sure to rename them accordingly (e.g., 01img, 02img, etc).
For example in NodeJS, to create a PDF file from all images that have the tag “animal”, and limit all images to a size of 400×600:
cloudinary.v2.uploader.multi("animal",
{ format: "pdf",
transformation: {
width: 400,
Height: 600
crop: "limit"}},
function(error,result) {console.log(result, error) });
Converting Office Documents to PDFs:
to automatically convert your Office documents, spreadsheets and presentations to PDF documents, Cloudinary offers the Aspose’s document conversion add-on.
You start by uploading the Office file to your Cloudinary account as a raw file and adding the raw_convert parameter with a value of `aspose`. Once the conversion has finished, a PDF with the same public ID is added to your Cloudinary account as an image resource type (the original still exists as a raw resource type).
cloudinary.v2.uploader.upload("sample.docx",
{ public_id: "sample_doc.docx",
resource_type: "raw",
raw_convert: "aspose" },
function(error, result){console.log(result);});
In the NodeJS example above, a new image (PDF) is added called sample_doc.docx, and can be delivered and/or transformed as any other PDF uploaded to your account.
Extracting Text from PDFs:
Cloudinary's OCR Text Detection and Extraction add-on allows you to extract and analyze text from PDFs, which can be useful for tagging, redacting, or analyzing content. To request the detected text, add the ocr parameter with a value of `adv_ocr:document`. For example, when using the upload method in NodeJS:
cloudinary.v2.uploader.upload("sample.pdf",
{ ocr: "adv_ocr" },
function(error, result) {console.log(result); });
Rasterizing PDFs
Rasterizing a PDF is useful if you still want to deliver a PDF file, but also want to resize the file, or maybe add an overlay. Rasterization reduces the PDF to a single flat pixelated layer (as opposed to vector-based multiple layers). Simply add the rasterize flag to the dynamic URL (fl_rasterize) and add any resizing or overlays as desired.
The following example in NodeJS rasterizes a PDF, scales it down to a width of 800 pixels and adds the Cloudinary icon to the top right corner of each page:
cloudinary.image("multi_page_pdf.pdf", {transformation: [
{flags: "rasterize"},
{width: 800, crop: "scale"},
{overlay: "cloudinary_icon", gravity: "north_east", width: 100}
]})
**Important**: In our efforts to make sure no malicious files are distributed through Cloudinary, Free customers can't deliver PDF files by default.
If you are on a Free plan and need to deliver files in these formats, you can enable Allow delivery of PDF and ZIP files in the Product environment Security settings. This option is enabled by default for all paid plans.
Comments
0 comments
Please sign in to leave a comment.