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.

Reply
  • 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.

Children