0

I have created one custom action to convert document to pdf. It worked fine but I want to download converted pdf on click of same custom action , I mean I want to convert and download document on clicking of custom action.How can do that? I have tried following code.

newdoc = document.transformDocument("application/pdf"); newdoc.save();
Sanjay
  • 2,481
  • 1
  • 13
  • 28
  • Call this action via share javascript to Repo Webscript for transformation. Once the file is generated , from client side get the nodeRef and opent the download url like http://localhost:8080/share/proxy/alfresco/slingshot/node/content/workspace/SpacesStore/34104a8f-6385-49e2-9cb2-655939cdf0bf?a=true. Let me try to create sample for you. – Muralidharan Deenathayalan Dec 16 '16 at 11:57

1 Answers1

3

Here you go and you need to pass your actual nodeRef values. Added new document action in share-custom-config.xml

<action id="convert-to-pdf-download" type="javascript" label="Download As PDF" icon="document-download">
<param name="function">onTransformToPDFAndDownload</param>
</action>

<actionGroups>
<actionGroup id="document-browse">                  
<action index="107" id="convert-to-pdf-download" />
</actionGroup>
<actionGroup id="document-details">   
<action index="107" id="convert-to-pdf-download" />
</actionGroup>   
</actionGroups>

Now you need to inject your javascript like below and you need to pass the original document's nodeRef and I have hard-code here.

onTransformToPDFAndDownload: function dla_onTransformToPDFAndDownload(record) {

          Alfresco.util.Ajax.request(
          {
            url: Alfresco.constants.PROXY_URI + "com/quanticate/quanticliq/transformer/transform?noderef=workspace://SpacesStore/ec0ca4cf-9ea4-4c12-8f2c-5b0c406e454b",
            successCallback:
            {
               fn: function onTransformAction_success(response)
               {
                    debugger;
                    var pdfNodeRef = response.json.pdfNodeRef;
                    pdfNodeRef = pdfNodeRef.replace("://","/");

                  window.open(Alfresco.constants.PROXY_URI + "slingshot/node/content/" + pdfNodeRef +"?a=true");
               },
               scope: this
            },
            failureCallback:
            {
               fn: function onTransformAction_failure(response)
               {
                  Alfresco.util.PopupManager.displayMessage(
                  {
                     text: "Something went wrong,please try again later"
                  });
               },
               scope: this
            }
         }); 
      }

On the Repowebscript, convert.get.desc.xml

<webscript>
   <shortname>toPDF</shortname>
   <desciption>Return PDF Node</desciption>
   <url>/com/quanticate/quanticliq/transformer/transform</url>
   <authentication>user</authentication>
   <format default="json">any</format>
</webscript>

convert.get.json.ftl

\"{\"pdfNodeRef\" :\"${pdfNodeRef.nodeRef}\"}\"

convert.get.js

function main()
{
   var json = "{}";

    var docNode = search.findNode("workspace://SpacesStore/ec0ca4cf-9ea4-4c12-8f2c-5b0c406e454b");   
    var nodeTrans = docNode.transformDocument("application/pdf");
    model.pdfNodeRef =  nodeTrans.nodeRef;  
}
main();

When you click Download As PDF, the PDF document will be generated, placed info document library (or where the original document is present) and will be downloaded automatically. You need to check for the existing PDF files exists or not also.

  • is it possible to delete generated pdf after downloading?if yes then how? – Sanjay Dec 17 '16 at 04:40
  • You can follow, how the "Delete Document" action works, refer _onActionDeleteConfirm method in C:\Alfresco5\tomcat\webapps\share\components\documentlibrary\actions.js file. You need to call the webscript using AJAX Delete request and pass the PDF file noderef and ensure the PDF document is downloaded, but the ideal solution should be you need to convert the doc to PDF in the server side, save them into alfresco temp location and download it. – Muralidharan Deenathayalan Dec 17 '16 at 06:16
  • , can i override _onActionDeleteConfirm.call(this, record) in my custion action function? if Yes Then How? i have tried using this way but it's not working. Alfresco.doclib.Actions.prototype._onActionDeleteConfirm.call(this, record1); – Sanjay Dec 22 '16 at 04:52
  • @ Muralidharan Deenathayalan Can i create my own policy/behavior? my point is i want to disable download if user has once dowload the document. – Sanjay Dec 27 '16 at 04:37