PDF export

Hi,

We want to have a pdf export functionality for our wikis.

In order to do this, I have constructed a widget extension that transforms a html string into a pdf, this is called in the export.vm

I parse the wiki and make an ajax call that posts the html string to a export.vm

         //htmlContent - the parsed wiki html
         jQuery.telligent.evolution.post({
            url: '$core_v2_encoding.JavascriptEncode($core_v2_widget.GetExecutedFileUrl('export.vm'))',
            data: {
                w_html: htmlContent
            },
            success: function(response){
                var blob = new Blob([response], {type: "application/pdf"});
                saveAs(blob, "file.pdf");
            }
        });

In the export.vm I call the widget extension that returns a System.Byte[]

#if ($core_v2_page.IsPost)
    #set($html = $core_v2_page.GetFormValue('w_html'))

    ##custom widget extension
    #set($pdf = $sdl_exportToPdf.ExportToPdf($html))

    $pdf

#end

In the ajax call success method above, I try to transform the response into a blob and 

save it using FileSaver.js, but the pdf file cannot open up correctly.

Am I not posting back the $pdf correctly? what am I missing here ?

Regards,

Silviu 

Parents
  • You probably won't be able to download a file like that via AJAX.

    Instead of AJAX, you could continue using your export.vm, but craft URLs that point directly to it. You could still use GetExecutedFileUrl, for example, but just not in AJAX.

    Then, when navigating to export.vm directly in the browser, you could still POST to it, but it would require crafting a full form POST instead.

    An easier option would be to navigate to export.vm via GET instead. But then you wouldn't be able to pass the HTML content since it could be too long. Instead, just pass the ContentId of the wiki page and load the page's content within export.vm.

    Finally, don't forget to set the proper content type with $core_v2_page.SetContentType("application/pdf")

    For other exports in Community, we also typically use widget extensions, but in those extensions, we write the entire response, including the headers, data, and Content-Disposition in order to specify the file name to the browser as well as explicitly control cache expirations.

  • Thanks for the fast response. I would to focus on the most simple solution, that's why I would like to discuss your last statement. After my widget extension returns the pdf as a array of byte, how can I create a download link in the export.vm velocity script, is that possible?

  • You would still use a URL from GetExecutedFileUrl for export.vm, but instead just link directly to it instead of AJAX. You'd also want to pass any wiki page identifiers to it so it can load the wiki page inside of it. And don't return an array of bytes from your extension, but rather, write directly to the HTTP response within that, including all the other headers you'd want to include (file name, pdf mime type). That portion would be not unlike generating downloadable responses in ASP.NET anywhere else, regardless of Telligent Community.

Reply
  • You would still use a URL from GetExecutedFileUrl for export.vm, but instead just link directly to it instead of AJAX. You'd also want to pass any wiki page identifiers to it so it can load the wiki page inside of it. And don't return an array of bytes from your extension, but rather, write directly to the HTTP response within that, including all the other headers you'd want to include (file name, pdf mime type). That portion would be not unlike generating downloadable responses in ASP.NET anywhere else, regardless of Telligent Community.

Children
No Data