Skip to content

Commit

Permalink
Pdf Export - enhance output (#167)
Browse files Browse the repository at this point in the history
* make functions private ; remove redundant initializers

* remove Chartinfo coding - it was always null

* make pdfheight class variable again - used in other functions too

* remove non-used  variables/assignments

* Copyright / Imports

* make global myparser --> OSParser instead of AllParser to use getHostName etc .. there is no other ParserType than OSParser

* PDF document properties

* IndexPage will show Hostname, OsType, Date, Timeline,

* On Each Page
- reposition page number to avoid overlap in footer
- show hostname and date in header

* checkStype import
  • Loading branch information
Pitterling committed Nov 30, 2018
1 parent 798e224 commit 4bbaff1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
83 changes: 53 additions & 30 deletions src/main/java/net/atomique/ksar/export/FilePDF.java
@@ -1,16 +1,18 @@
/*
* Copyright 2017 The kSAR Project. All rights reserved.
* Copyright 2018 The kSAR Project. All rights reserved.
* See the LICENSE file in the project root for more information.
*/

package net.atomique.ksar.export;

import static com.itextpdf.text.FontFactory.COURIER;
import static com.itextpdf.text.FontFactory.getFont;

import com.itextpdf.awt.PdfGraphics2D;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
Expand All @@ -19,6 +21,7 @@
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

import net.atomique.ksar.Config;
import net.atomique.ksar.VersionNumber;
import net.atomique.ksar.graph.Graph;
Expand All @@ -27,14 +30,16 @@
import net.atomique.ksar.ui.ParentNodeInfo;
import net.atomique.ksar.ui.SortedTreeNode;
import net.atomique.ksar.ui.TreeNodeInfo;
import org.jfree.chart.ChartRenderingInfo;

import org.jfree.chart.JFreeChart;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -46,7 +51,7 @@ public class FilePDF extends PdfPageEventHelper implements Runnable {

private static final Logger log = LoggerFactory.getLogger(FilePDF.class);

public FilePDF(String filename, kSar hissar) {
private FilePDF(String filename, kSar hissar) {
pdffilename = filename;
mysar = hissar;
}
Expand Down Expand Up @@ -80,7 +85,7 @@ public void run() {
break;
}

float pdfheight = document.getPageSize().getHeight();
pdfheight = document.getPageSize().getHeight();
pdfwidth = document.getPageSize().getWidth();
pageheight = pdfheight - (2 * pdfmargins);
pagewidth = pdfwidth - (2 * pdfmargins);
Expand All @@ -95,8 +100,8 @@ public void run() {

// document parameter before open
document.addTitle("kSar Grapher");
//document.addSubject("Sar output of " + mysar.hostName);
//document.addKeywords("http://ksar.atomique.net/ ");
//document.addSubject("SAR Statistics of " + mysar.hostName);
//document.addKeywords("https://github.com/vlsi/ksar");
//document.addKeywords(mysar.hostName);
//document.addKeywords(mysar.myOS.sarStartDate);
//document.addKeywords(mysar.myOS.sarEndDate);
Expand All @@ -108,7 +113,7 @@ public void run() {
pdfcb = writer.getDirectContent();
PdfOutline root = pdfcb.getRootOutline();

IndexPage(writer, document);
IndexPage(document);

export_treenode(mysar.graphtree, root);

Expand All @@ -121,8 +126,7 @@ public void run() {

}


public void export_treenode(SortedTreeNode node, PdfOutline root) {
private void export_treenode(SortedTreeNode node, PdfOutline root) {
int num = node.getChildCount();
if (num > 0) {
Object obj1 = node.getUserObject();
Expand All @@ -143,7 +147,7 @@ public void export_treenode(SortedTreeNode node, PdfOutline root) {
TreeNodeInfo tmpnode = (TreeNodeInfo) obj1;
Graph nodeobj = tmpnode.getNode_object();
if (nodeobj.isPrintSelected()) {
root = new PdfOutline(root, new PdfDestination(PdfDestination.FIT), nodeobj.getTitle());
new PdfOutline(root, new PdfDestination(PdfDestination.FIT), nodeobj.getTitle());
update_ui();
addchart(writer, nodeobj);
document.newPage();
Expand All @@ -163,49 +167,68 @@ private void update_ui() {

public void onEndPage(PdfWriter writer, Document document) {
try {
String text = "Page " + writer.getPageNumber() + "/" + total_pages;

int pageNumber = writer.getPageNumber();
String text = "Page " + pageNumber + "/" + total_pages;
String hostName = mysar.myparser.gethostName();
String date = mysar.myparser.getDate();

pdfcb.beginText();
pdfcb.setFontAndSize(bf, 10);
pdfcb.setColorFill(new BaseColor(0x00, 0x00, 0x00));
pdfcb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text, ((pdfwidth - pdfmargins) - 10),
10 + pdfmargins, 0);

if ( pageNumber > 1) {
pdfcb.showTextAligned(PdfContentByte.ALIGN_LEFT, hostName, pdfmargins, pdfheight - pdfmargins, 0);
pdfcb.showTextAligned(PdfContentByte.ALIGN_RIGHT, date, pdfwidth - pdfmargins, pdfheight - pdfmargins, 0);
}

pdfcb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text,pdfwidth - pdfmargins,pdfmargins - 5 ,0);
pdfcb.endText();
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}

public int addchart(PdfWriter writer, Graph graph) {
private void addchart(PdfWriter writer, Graph graph) {
JFreeChart chart =
graph.getgraph(mysar.myparser.getStartOfGraph(), mysar.myparser.getEndOfGraph());
PdfTemplate pdftpl = pdfcb.createTemplate(pagewidth, pageheight);
Graphics2D g2d = new PdfGraphics2D(pdftpl, pagewidth, pageheight);
Double r2d = new Rectangle2D.Double(0, 0, pagewidth, pageheight);
chart.draw(g2d, r2d, chartinfo);
chart.draw(g2d, r2d);
g2d.dispose();
pdfcb.addTemplate(pdftpl, pdfmargins, pdfmargins);
try {
writer.releaseTemplate(pdftpl);
} catch (IOException ioe) {
log.error("Unable to write to : {}", pdffilename);
}
return 0;
}

public void IndexPage(PdfWriter writer, Document document) {
private void IndexPage(Document document) {
try {
float pdfCenter = ((pdfwidth - pdfmargins) / 2 );

String title = "SAR Statistics";
String t_date = "on " + mysar.myparser.getDate();
String hostName = "for " + mysar.myparser.gethostName();
String osType = mysar.myparser.getOstype();
String graphStart = mysar.myparser.getStartOfGraph().toString();
String graphEnd = mysar.myparser.getEndOfGraph().toString();

String title = "Statistics";
String t_date = "On " + mysar.myparser.getDate();
pdfcb.beginText();
pdfcb.setFontAndSize(bf, 48);
pdfcb.setFontAndSize(bf, 40);
pdfcb.setColorFill(new BaseColor(0x00, 0x00, 0x00));
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, title, ((pdfwidth - pdfmargins) / 2), 500,
0);
pdfcb.setFontAndSize(bf, 36);
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, t_date, ((pdfwidth - pdfmargins) / 2), 300,
0);
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, title, pdfCenter, 500, 0);

pdfcb.setFontAndSize(bf, 32);
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, hostName + " (" + osType + ")", pdfCenter, 400,0);
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, t_date, pdfCenter, 300,0);

pdfcb.setFontAndSize(bf, 20);
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, graphStart, pdfCenter, 200,0);
pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, graphEnd, pdfCenter, 150,0);

pdfcb.endText();
document.newPage();

Expand All @@ -216,17 +239,17 @@ public void IndexPage(PdfWriter writer, Document document) {

private int progress_info = 0;
private float pdfwidth;
private float pdfheight;
private int pdfmargins = 10;
private float pageheight;
private float pagewidth;
private int total_pages = 1; // page 1 (index)
private String pdffilename = null;
private String pdffilename;
private Document document = null;
private PdfWriter writer = null;
private PdfContentByte pdfcb;
private kSar mysar = null;
private BaseFont bf = FontFactory.getFont(FontFactory.COURIER).getCalculatedBaseFont(false);
private kSar mysar;
private BaseFont bf = getFont(COURIER).getCalculatedBaseFont(false);
private JProgressBar progress_bar = null;
private JDialog dialog = null;
private ChartRenderingInfo chartinfo = null;
}
4 changes: 2 additions & 2 deletions src/main/java/net/atomique/ksar/kSar.java
Expand Up @@ -120,7 +120,7 @@ public int parse(BufferedReader br) {
Class classtmp = GlobalOptions.getParser(ParserType);
if (classtmp != null) {
if (myparser == null) {
myparser = (AllParser) classtmp.newInstance();
myparser = (OSParser) classtmp.newInstance();
myparser.init(this, current_line);

continue;
Expand Down Expand Up @@ -241,7 +241,7 @@ public boolean isParsing() {
private String reload_action = "Empty";
private Thread launched_action = null;
private boolean action_interrupted = false;
public AllParser myparser = null;
public OSParser myparser = null;
private boolean Parsing = false;
public int total_graph = 0;
public SortedTreeNode graphtree = new SortedTreeNode("kSar");
Expand Down

0 comments on commit 4bbaff1

Please sign in to comment.