Skip to main content

How can I download my upload PDF file?

Comments

14 comments

  • Stephen Doyle

    Hi Kojiro,

    If you want to create a Cloudinary URL which will request that the specified file is handled by the web browser as a download, and should not be opened directly, we have a parameter you can set which will request this.

    If you're using our SDK to create the URL, please add the 'flags' parameter and add 'attachment' as a value. 
    If you're creating your URLs manually, you can specify this flag in the URL with fl_attachment.

    For example, this link should open the sample image in our demo account: image
    And this link should request that the browser treats the file as a download: image

    You can read more about this flag and the options (which include specifying the filename to use when saving the file) here: https://cloudinary.com/documentation/image_transformation_reference#flags_parameter

    Please let me know if that helps, and if you have additional questions

    2
  • chikara

    HI Stephen, 

    I am using the following code to get a download of a multi-page PDF file, and am trying to use the PDF file within my Rails code. However, I get an error that says "undefined method ... for String." It seems Cloudinary is returning a string and not a file. How do I get that into a file?

    pdf = Cloudinary::Downloader.download("file_name", :flags => :attachment)

    I tried decoding the returned string using Base64, but I get a "dynamic assignment error." 

     

    0
  • Shirly Manor

    Hi,

    When adding the flag attachment to the file it will download it when accessing it. For example:

    https://res.cloudinary.com/demo/image/upload/fl_attachment/multi_page_pdf.pdf

    You can create an image tag:

    cl_image_tag("multi_page_pdf.pdf", :flag=>"attachment")

    Let me know if that can works,
    0
  • chikara

    Thanks for the response Shirly. I got it to work with the following code:

    download = Cloudinary::Downloader.download("public_id", :flags => :attachment)
    # Create new File instance for writing in binary
    pdf = File.new("path to file", "wb")
    pdf.write(download)
    pdf.close

    Indeed, the flags => attachment works.

    1
  • Shirly Manor

    Great! Thank you for the update and sharing your code!

    0
  • Amit Pallauri

    hello. can any one help me with understanding how we can attach this flag in react.

    cause in react SDK of cloudinary, we have to use 
    <Image>
        <Transform>

    but as i want to download a pdf file, i can't set it like this. having trouble with this

     

     

    0
  • Shirly Manor

    Hi Amit,

    You can create an image component:

     

    <Image publicId="sample.jpg" >

       <Transformation flags="attachment:pretty_flower" fetchFormat="auto" />

    </Image>
     
    However, inside an image, it will just show the image.
    0
  • Amit Pallauri

    hey @Shiley, thanks for your response 
    we get  publicid, secure_url as response after uploading the data in cloudinary. but the public id you used here (sample.jpg) in the Image tag doesn't look like the one i got in my response (5464241126). so is that response public id is same as the Image public id you used here ( sample.jpg) or this public id is just a name?

    and i got the same question for the attachment:pretty_flower also. is that pretty flower is just name or it has to be something which comes form the response after uploading?

    as you can see in above image, i want to make the image as clickable link so that on click i can download it. but i am not getting anywhere.

    0
  • Michal Kuperman

    Hi Amit.

    In order to achieve this please consider this code:

    ```
    <div>
    <CloudinaryContext cloudName="demo">
    <Image publicId="multi_page_pdf.pdf">
    <Transformation fetchFormat="auto" />
    </Image>
    </a>
    </CloudinaryContext>
    </div>
     
    ```
     
    The ״multi_page_pdf״ is the public_id of the image you would like to display and download."myPdf" is the name you set for the attachment file's name (this is optional if it is not set then the original filename is used as the attachment file name).
     
    Let us know if this works for you and if you need any further assistance.
     
    1
  • Amit Pallauri

    thank you for your answer michal. as you can see in the picture below, this is my backend code where i am trying to upload and this is the response i am getting. so here in secure_url the flags are not mentioned that's why your above answer can't be implemented to my saved data in database.

    so can you tell me if there is any way we can set flags of attachment in the sedcure_url while uploading itself. so that it gets saved like that in database and later while fetching that url, i can get an attachment.

    0
  • Michal Kuperman

    Hi Amit,

    You can create a transformed image upon upload by using eager transformations and then receive the transformed secure URL in the response under the eager value.

    You can read more about eager transformations here:

    https://cloudinary.com/documentation/transformations_on_upload#eager_transformations

    For example:

    ```

    Map result = cloudinary.uploader().upload("samplePdf.pdf", ObjectUtils.asMap(
    "eager", Arrays.asList(
    new Transformation().flags("attachment"))));

    ```

    And the response will include:

    ```

    eager=[{...secure_url=https://res.cloudinary.com/demo/image/upload/fl_attachment/v1601895305/samplePdf.pdf, 

    ```

    Let us know if this solves your issue

    0
  • Amit Pallauri

    Thank you so much Michal, it worked for me. here's the actual code. if anyone needs this in nodejs

    0
  • Michal Kuperman

    Great! Thanks for the update.

    0
  • Ritik Gupta

    Amit can you please share the code that you used in frontend to display the pdf file and then download it

     

    0

Post is closed for comments.