-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.document;\r
-\r
-import java.io.BufferedInputStream;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.net.URL;\r
-\r
-import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;\r
-import org.eclipse.mylyn.wikitext.mediawiki.core.MediaWikiLanguage;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.utils.FileUtils;\r
-import org.simantics.utils.ui.BundleUtils;\r
-import org.simantics.wiki.ui.editor.IExportable;\r
-\r
-import com.lowagie.text.Document;\r
-import com.lowagie.text.DocumentException;\r
-import com.lowagie.text.PageSize;\r
-import com.lowagie.text.Utilities;\r
-import com.lowagie.text.pdf.PdfContentByte;\r
-import com.lowagie.text.pdf.PdfImportedPage;\r
-import com.lowagie.text.pdf.PdfReader;\r
-import com.lowagie.text.pdf.PdfWriter;\r
-\r
-public class Exportable implements IExportable {\r
-\r
- final private String html;\r
- final private DocumentSettings settings;\r
-\r
- private static final String DEFAULT_CSS;\r
- public static final String DEFAULT_MODEL_CSS;\r
-\r
- private static String getBundleFileContents(String path, String defaultValue) {\r
- URL url = BundleUtils.find(Activator.getContext().getBundle(), path);\r
- if (url == null)\r
- return defaultValue;\r
- try (InputStream in = url.openStream()) {\r
- return FileUtils.getContents(in);\r
- } catch (IOException e) {\r
- Logger.defaultLogError(e);\r
- return defaultValue;\r
- }\r
- }\r
-\r
- static {\r
- DEFAULT_CSS = getBundleFileContents("simantics-wiki-documents.css", "");\r
- DEFAULT_MODEL_CSS = getBundleFileContents("simantics-wiki-documents-default-model.css", "");\r
- }\r
- \r
- public Exportable(ReadGraph graph, Resource res, String wiki, String css, DocumentSettings settings, boolean print) {\r
-\r
- try {\r
- wiki = DocumentDialect.INSTANCE.apply(graph, res, wiki);\r
- } catch (DatabaseException e) {\r
- Logger.defaultLogError(e);\r
- }\r
- \r
- this.settings = settings;\r
- \r
- MarkupParser markupParser = new MarkupParser();\r
- markupParser.setMarkupLanguage(new MediaWikiLanguage());\r
- String html = markupParser.parseToHtml(wiki);\r
- \r
- String width = "width:" + (210-settings.marginLeft-settings.marginRight) + "mm;";\r
-\r
- if(print) {\r
- html = html.replace("<body>", "<body style=\"background-color:#fff\"><div style=\"background-color:#FFF;" + width + "\">");\r
- html = html.replace("</body>", "</div></body>");\r
- } else {\r
- String div1 = "margin-left:3mm;margin-top:3mm;background-color:#FFF;width:210mm;"; \r
- String div2 = "background-color:#FFF;width:210mm;padding-top:" + settings.marginTop + "mm;";\r
- String div3 = "overflow-x:hidden;margin-left:" + settings.marginLeft + "mm;background-color:#FFF;" + width; \r
- html = html.replace("<body>", "<body><div style=\"" + div1 + "\"><div style=\"" + div2 + "\"><div style=\"" + div3 + "\">");\r
- html = html.replace("</body>", "</div></div></div></body>");\r
- }\r
- \r
- html = html.replace("<td>", "<td><div>");\r
- html = html.replace("<th>", "<th><div>");\r
- html = html.replace("</td>", "</div></td>");\r
- html = html.replace("</th>", "</div></th>");\r
- html = html.replace("</head>", "<style type=\"text/css\">\n" + DEFAULT_CSS + css + "\n</style>\n</head>\n");\r
- \r
- this.html = html;\r
- \r
- }\r
- \r
- public String getHTML() {\r
- return html;\r
- }\r
-\r
- @Override\r
- public void export(Document document, PdfWriter writer) throws DocumentException {\r
-\r
- File temp = Simantics.getTempfile("wikiPdfExport", "pdf");\r
- try {\r
- \r
- temp.getParentFile().mkdirs();\r
- PhantomJSDriver.print(html, settings, temp);\r
- \r
- PdfContentByte cb = writer.getDirectContent();\r
- PdfReader reader = new PdfReader(new BufferedInputStream(new FileInputStream(temp)));\r
- for (int i = 1; i <= reader.getNumberOfPages(); i++) {\r
- document.setPageSize(PageSize.A4);\r
- document.newPage();\r
- //import the page from source pdf\r
- PdfImportedPage page = writer.getImportedPage(reader, i);\r
- //add the page to the destination pdf\r
- float pts = Utilities.millimetersToPoints(10);\r
- cb.addTemplate(page, pts, pts);\r
- }\r
-\r
- } catch (IOException e) {\r
-\r
- throw new DocumentException(e);\r
- \r
- }\r
-\r
- }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.document;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage;
+import org.eclipse.mylyn.wikitext.parser.MarkupParser;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.utils.FileUtils;
+import org.simantics.utils.ui.BundleUtils;
+import org.simantics.wiki.ui.editor.IExportable;
+
+import com.lowagie.text.Document;
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.PageSize;
+import com.lowagie.text.Utilities;
+import com.lowagie.text.pdf.PdfContentByte;
+import com.lowagie.text.pdf.PdfImportedPage;
+import com.lowagie.text.pdf.PdfReader;
+import com.lowagie.text.pdf.PdfWriter;
+
+public class Exportable implements IExportable {
+
+ final private String html;
+ final private DocumentSettings settings;
+
+ private static final String DEFAULT_CSS;
+ public static final String DEFAULT_MODEL_CSS;
+
+ private static String getBundleFileContents(String path, String defaultValue) {
+ URL url = BundleUtils.find(Activator.getContext().getBundle(), path);
+ if (url == null)
+ return defaultValue;
+ try (InputStream in = url.openStream()) {
+ return FileUtils.getContents(in);
+ } catch (IOException e) {
+ Logger.defaultLogError(e);
+ return defaultValue;
+ }
+ }
+
+ static {
+ DEFAULT_CSS = getBundleFileContents("simantics-wiki-documents.css", "");
+ DEFAULT_MODEL_CSS = getBundleFileContents("simantics-wiki-documents-default-model.css", "");
+ }
+
+ public Exportable(ReadGraph graph, Resource res, String wiki, String css, DocumentSettings settings, boolean print) {
+
+ try {
+ wiki = DocumentDialect.INSTANCE.apply(graph, res, wiki);
+ } catch (DatabaseException e) {
+ Logger.defaultLogError(e);
+ }
+
+ this.settings = settings;
+
+ MarkupParser markupParser = new MarkupParser();
+ markupParser.setMarkupLanguage(new MediaWikiLanguage());
+ String html = markupParser.parseToHtml(wiki);
+
+ String width = "width:" + (210-settings.marginLeft-settings.marginRight) + "mm;";
+
+ if(print) {
+ html = html.replace("<body>", "<body style=\"background-color:#fff\"><div style=\"background-color:#FFF;" + width + "\">");
+ html = html.replace("</body>", "</div></body>");
+ } else {
+ String div1 = "margin-left:3mm;margin-top:3mm;background-color:#FFF;width:210mm;";
+ String div2 = "background-color:#FFF;width:210mm;padding-top:" + settings.marginTop + "mm;";
+ String div3 = "overflow-x:hidden;margin-left:" + settings.marginLeft + "mm;background-color:#FFF;" + width;
+ html = html.replace("<body>", "<body><div style=\"" + div1 + "\"><div style=\"" + div2 + "\"><div style=\"" + div3 + "\">");
+ html = html.replace("</body>", "</div></div></div></body>");
+ }
+
+ html = html.replace("<td>", "<td><div>");
+ html = html.replace("<th>", "<th><div>");
+ html = html.replace("</td>", "</div></td>");
+ html = html.replace("</th>", "</div></th>");
+ html = html.replace("</head>", "<style type=\"text/css\">\n" + DEFAULT_CSS + css + "\n</style>\n</head>\n");
+
+ this.html = html;
+
+ }
+
+ public String getHTML() {
+ return html;
+ }
+
+ @Override
+ public int export(Document document, PdfWriter writer) throws DocumentException {
+
+ File temp = Simantics.getTempfile("wikiPdfExport", "pdf");
+ try {
+
+ temp.getParentFile().mkdirs();
+ PhantomJSDriver.print(html, settings, temp);
+
+ int result = 0;
+ PdfContentByte cb = writer.getDirectContent();
+ PdfReader reader = new PdfReader(new BufferedInputStream(new FileInputStream(temp)));
+ for (int i = 1; i <= reader.getNumberOfPages(); i++) {
+ document.setPageSize(PageSize.A4);
+ document.newPage();
+ //import the page from source pdf
+ PdfImportedPage page = writer.getImportedPage(reader, i);
+ //add the page to the destination pdf
+ float pts = Utilities.millimetersToPoints(10);
+ cb.addTemplate(page, pts, pts);
+ ++result;
+ }
+
+ return result;
+ } catch (IOException e) {
+
+ throw new DocumentException(e);
+
+ }
+
+ }
+