Search in sources :

Example 1 with PdfExportImpl

use of com.xpn.xwiki.pdf.impl.PdfExportImpl in project xwiki-platform by xwiki.

the class ExportAction method export.

private String export(String format, XWikiContext context) throws XWikiException, IOException {
    // We currently use the PDF export infrastructure but we have to redesign the export code.
    XWikiURLFactory urlFactory = new OfficeExporterURLFactory();
    PdfExport exporter = new OfficeExporter();
    // Check if the office exporter supports the specified format.
    ExportType exportType = ((OfficeExporter) exporter).getExportType(format);
    // Note 2: we don't use the office server for PDF exports since it doesn't work OOB. Instead we use FOP.
    if ("pdf".equalsIgnoreCase(format)) {
        // The export format is PDF or the office converter can't be used (either it doesn't support the specified
        // format or the office server is not started).
        urlFactory = new PdfURLFactory();
        exporter = new PdfExportImpl();
        exportType = ExportType.PDF;
    } else if (exportType == null) {
        context.put("message", "core.export.formatUnknown");
        return "exception";
    }
    urlFactory.init(context);
    context.setURLFactory(urlFactory);
    handleRevision(context);
    XWikiDocument doc = context.getDoc();
    context.getResponse().setContentType(exportType.getMimeType());
    // Compute the name of the export. Since it's gong to be saved on the user's file system it needs to be a valid
    // File name. Thus we use the "path" serializer but replace the "/" separator by "_" since we're not computing
    // a directory hierarchy but a file name
    EntityReferenceSerializer<String> serializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "path");
    String filename = serializer.serialize(doc.getDocumentReference()).replaceAll("/", "_");
    // Make sure we don't go over 255 chars since several filesystems don't support filename longer than that!
    filename = StringUtils.abbreviateMiddle(filename, "__", 255);
    context.getResponse().addHeader("Content-disposition", String.format("inline; filename=%s.%s", filename, exportType.getExtension()));
    exporter.export(doc, context.getResponse().getOutputStream(), exportType, context);
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) OfficeExporter(com.xpn.xwiki.internal.export.OfficeExporter) PdfExport(com.xpn.xwiki.pdf.api.PdfExport) ExportType(com.xpn.xwiki.pdf.api.PdfExport.ExportType) OfficeExporterURLFactory(com.xpn.xwiki.internal.export.OfficeExporterURLFactory) PdfURLFactory(com.xpn.xwiki.pdf.impl.PdfURLFactory) PdfExportImpl(com.xpn.xwiki.pdf.impl.PdfExportImpl)

Example 2 with PdfExportImpl

use of com.xpn.xwiki.pdf.impl.PdfExportImpl in project xwiki-platform by xwiki.

the class PDFAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    XWikiURLFactory urlf = context.getWiki().getURLFactoryService().createURLFactory(XWikiContext.MODE_PDF, context);
    context.setURLFactory(urlf);
    PdfExportImpl pdfexport = new PdfExportImpl();
    XWikiDocument doc = context.getDoc();
    handleRevision(context);
    try {
        context.getResponse().setContentType(ExportType.PDF.getMimeType());
        // Compute the name of the export. Since it's gong to be saved on the user's file system it needs to be a
        // valid File name. Thus we use the "path" serializer but replace the "/" separator by "_" since we're not
        // computing a directory hierarchy but a file name
        EntityReferenceSerializer<String> serializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "path");
        String filename = serializer.serialize(doc.getDocumentReference()).replaceAll("/", "_");
        // Make sure we don't go over 255 chars since several filesystems don't support filename longer than that!
        filename = StringUtils.abbreviateMiddle(filename, "__", 255);
        context.getResponse().addHeader("Content-disposition", String.format("inline; filename=%s.%s", filename, ExportType.PDF.getExtension()));
        pdfexport.export(doc, context.getResponse().getOutputStream(), ExportType.PDF, context);
    } catch (IOException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e);
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) IOException(java.io.IOException) PdfExportImpl(com.xpn.xwiki.pdf.impl.PdfExportImpl) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 PdfExportImpl (com.xpn.xwiki.pdf.impl.PdfExportImpl)2 XWikiException (com.xpn.xwiki.XWikiException)1 OfficeExporter (com.xpn.xwiki.internal.export.OfficeExporter)1 OfficeExporterURLFactory (com.xpn.xwiki.internal.export.OfficeExporterURLFactory)1 PdfExport (com.xpn.xwiki.pdf.api.PdfExport)1 ExportType (com.xpn.xwiki.pdf.api.PdfExport.ExportType)1 PdfURLFactory (com.xpn.xwiki.pdf.impl.PdfURLFactory)1 IOException (java.io.IOException)1