5 Replies Latest reply on Jun 10, 2013 8:06 AM by iyer.s

    Creating PDF programmaticaly within DAM

    iyer.s Level 1

      I have the below requirement to implement within CQ -

      a) End user will select a PDF file from the form input file type field.

      b) I will have to recreate this PDF file within DAM.

      c) I have been using the AssetManager to create the same, with no luck, Below is the code snippet,

      the file gets created, with no content, what am i doing wrong?

       

                  InputStream is = new ByteArrayInputStream(fileContent.getBytes());

                  String fileName = "/content/dam/ani/us/pdf/" + UUID.randomUUID().toString() + ".pdf";

       

                  ResourceResolver resolver = slingRequest.getResourceResolver();

                  AssetManager manager = resolver.adaptTo(AssetManager.class);

                  Asset asset = manager.createAsset(fileName, is, "application/pdf", true);

       

      - fileContent string has the entire PDF file content

       

      Thanks

      S

        • 1. Re: Creating PDF programmaticaly within DAM
          justin_at_adobe Adobe Employee

          It may or may not be the issue, but it seems very strange for "fileContent" to be a string. If it is literally a file, why not use FileInputStream?

          • 2. Re: Creating PDF programmaticaly within DAM
            iyer.s Level 1

            Hi Justin,

             

            I'm capturing the PDF  from an HTML file input field and post it to the next layer of the application where i create the PDF within DAM using the API. 

            Now, when i post the html multipart form (where i capture the PDF file), the content of the file is posted to the target, and i can get it from the parameterMap (as below).

            Map<String, Object> params = request.getParameterMap();

            String[] fileContent = (String[]) params.get("uploadFile");

            There is no way i can get the full path to the file (due to security restrictions on the browser) and write code to read the content as stream and then pass it to the API to create at the DAM end.

            I also tried using Jersey multi-part, by adding the content to the header of the request, and then post to the backend service, that handles the processing, but, I got really sucked into the OSGi bundle hell for jersey-multipart and it's dependent bundles, this would have been the most ideal solution path.

            Any suggestions /pointers to solve this issue much appreciated.

             

            Best,

            Sivaram

            • 3. Re: Creating PDF programmaticaly within DAM
              justin_at_adobe Adobe Employee

              The JavaEE Servlet API doesn't handle file uploads.

               

              Instead, you can use the Sling API to do:

              RequestParameter param = request.getRequestParameter("uploadFile");

               

              // ensure we have a file upload

              if (!param.isFormField()) {

                  InputStream stream = param.getInputStream();

                 // TODO - pass the input stream to the Asset API

              } else {

                  // TODO - create some kind of error response

              }

              • 4. Re: Creating PDF programmaticaly within DAM
                iyer.s Level 1

                Perfect - I will try and let you know.

                 

                Thanks and Best@!

                • 5. Re: Creating PDF programmaticaly within DAM
                  iyer.s Level 1

                  Justin,

                   

                  Thank you so much for your suggestion, the solution you mentioned worked, and I was able to create the PDF within DAM.

                   

                  Thanks again!

                   

                  Best,

                  Sivaram