001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Gabriel Barata <gbarata@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.platform.preview.adapter;
021
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.Blobs;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.platform.preview.api.PreviewException;
026import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
027
028import java.util.ArrayList;
029import java.util.List;
030
031/**
032 * @since 8.2
033 */
034public class PdfPreviewer extends AbstractPreviewer implements MimeTypePreviewer {
035
036    public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
037        List<Blob> blobResults = new ArrayList<Blob>();
038        StringBuffer htmlPage = new StringBuffer();
039        String basePath = VirtualHostHelper.getContextPathProperty();
040
041        htmlPage.append("<script src=\"" + basePath + "/bower_components/webcomponentsjs/webcomponents-lite.js\"></script>");
042        htmlPage.append("<link rel=\"import\" href=\"" + basePath + "/viewers/nuxeo-pdf-viewer.vulcanized.html\">");
043
044        htmlPage.append("<style>");
045        htmlPage.append("nuxeo-pdf-viewer {");
046        htmlPage.append("weight: 100%;");
047        htmlPage.append("height: 100%;");
048        htmlPage.append("--nuxeo-pdf-viewer-iframe: {");
049        htmlPage.append("min-height: 500px; }}");
050        htmlPage.append("</style>");
051        htmlPage.append("</head><body>");
052
053        htmlPage.append("<nuxeo-pdf-viewer src=\"pdf\"></nuxeo-pdf-viewer>");
054        Blob mainBlob = Blobs.createBlob(htmlPage.toString(), "text/html", null, "index.html");
055        blob.setFilename("pdf");
056        blobResults.add(mainBlob);
057        blobResults.add(blob);
058
059        return blobResults;
060    }
061}