1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document;
14 import java.io.BufferedInputStream;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
21 import org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage;
22 import org.eclipse.mylyn.wikitext.parser.MarkupParser;
23 import org.simantics.Simantics;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.utils.Logger;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.utils.FileUtils;
29 import org.simantics.utils.ui.BundleUtils;
30 import org.simantics.wiki.ui.editor.IExportable;
32 import com.lowagie.text.Document;
33 import com.lowagie.text.DocumentException;
34 import com.lowagie.text.PageSize;
35 import com.lowagie.text.Utilities;
36 import com.lowagie.text.pdf.PdfContentByte;
37 import com.lowagie.text.pdf.PdfImportedPage;
38 import com.lowagie.text.pdf.PdfReader;
39 import com.lowagie.text.pdf.PdfWriter;
41 public class Exportable implements IExportable {
43 final private String html;
44 final private DocumentSettings settings;
46 private static final String DEFAULT_CSS;
47 public static final String DEFAULT_MODEL_CSS;
49 private static String getBundleFileContents(String path, String defaultValue) {
50 URL url = BundleUtils.find(Activator.getContext().getBundle(), path);
53 try (InputStream in = url.openStream()) {
54 return FileUtils.getContents(in);
55 } catch (IOException e) {
56 Logger.defaultLogError(e);
62 DEFAULT_CSS = getBundleFileContents("simantics-wiki-documents.css", "");
63 DEFAULT_MODEL_CSS = getBundleFileContents("simantics-wiki-documents-default-model.css", "");
66 public Exportable(ReadGraph graph, Resource res, String wiki, String css, DocumentSettings settings, boolean print) {
69 wiki = DocumentDialect.INSTANCE.apply(graph, res, wiki);
70 } catch (DatabaseException e) {
71 Logger.defaultLogError(e);
74 this.settings = settings;
76 MarkupParser markupParser = new MarkupParser();
77 markupParser.setMarkupLanguage(new MediaWikiLanguage());
78 String html = markupParser.parseToHtml(wiki);
80 String width = "width:" + (210-settings.marginLeft-settings.marginRight) + "mm;";
83 html = html.replace("<body>", "<body style=\"background-color:#fff\"><div style=\"background-color:#FFF;" + width + "\">");
84 html = html.replace("</body>", "</div></body>");
86 String div1 = "margin-left:3mm;margin-top:3mm;background-color:#FFF;width:210mm;";
87 String div2 = "background-color:#FFF;width:210mm;padding-top:" + settings.marginTop + "mm;";
88 String div3 = "overflow-x:hidden;margin-left:" + settings.marginLeft + "mm;background-color:#FFF;" + width;
89 html = html.replace("<body>", "<body><div style=\"" + div1 + "\"><div style=\"" + div2 + "\"><div style=\"" + div3 + "\">");
90 html = html.replace("</body>", "</div></div></div></body>");
93 html = html.replace("<td>", "<td><div>");
94 html = html.replace("<th>", "<th><div>");
95 html = html.replace("</td>", "</div></td>");
96 html = html.replace("</th>", "</div></th>");
97 html = html.replace("</head>", "<style type=\"text/css\">\n" + DEFAULT_CSS + css + "\n</style>\n</head>\n");
103 public String getHTML() {
108 public int export(Document document, PdfWriter writer) throws DocumentException {
110 File temp = Simantics.getTempfile("wikiPdfExport", "pdf");
113 temp.getParentFile().mkdirs();
114 PhantomJSDriver.print(html, settings, temp);
117 PdfContentByte cb = writer.getDirectContent();
118 PdfReader reader = new PdfReader(new BufferedInputStream(new FileInputStream(temp)));
119 for (int i = 1; i <= reader.getNumberOfPages(); i++) {
120 document.setPageSize(PageSize.A4);
122 //import the page from source pdf
123 PdfImportedPage page = writer.getImportedPage(reader, i);
124 //add the page to the destination pdf
125 float pts = Utilities.millimetersToPoints(10);
126 cb.addTemplate(page, pts, pts);
131 } catch (IOException e) {
133 throw new DocumentException(e);