-
Notifications
You must be signed in to change notification settings - Fork 148
Refactor PDF exports for headless printing #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sixlettervariables
wants to merge
9
commits into
MegaMek:master
Choose a base branch
from
sixlettervariables:refactor-pdf-exports
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8196218
Allow retargeting RS and Fluff paths
sixlettervariables dd20c35
Use CConfig::getRecordSheetsPath
sixlettervariables 226b7f3
Use CConfig::getFluffImagesPath()
sixlettervariables b8b76d7
Add record sheet book builder
sixlettervariables 2c47d11
Switch to RecordSheetBookBuilder
sixlettervariables 1de3948
Fix bug in RecordSheetBookBuilder::addEntities
sixlettervariables 3560a66
Refactor logic into PdfRecordSheetExporter
sixlettervariables ffa7714
Upgrade to FOP 2.5
sixlettervariables 076fac3
Address review comments
sixlettervariables File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package megameklab.com.printing; | ||
|
||
import java.awt.print.PageFormat; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import org.apache.fop.configuration.Configuration; | ||
import org.apache.fop.configuration.ConfigurationException; | ||
import org.apache.fop.configuration.DefaultConfigurationBuilder; | ||
import org.apache.batik.transcoder.TranscoderException; | ||
import org.apache.pdfbox.io.MemoryUsageSetting; | ||
import org.apache.pdfbox.multipdf.PDFMergerUtility; | ||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline; | ||
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; | ||
import org.xml.sax.SAXException; | ||
|
||
import megamek.common.annotations.Nullable; | ||
|
||
public class PdfRecordSheetExporter { | ||
private final MemoryUsageSetting memoryUsageSetting; | ||
private final Configuration cfg; | ||
|
||
public PdfRecordSheetExporter() { | ||
this(MemoryUsageSetting.setupTempFileOnly(), null); | ||
} | ||
|
||
public PdfRecordSheetExporter(MemoryUsageSetting memoryUsageSetting, @Nullable Configuration cfg) { | ||
this.memoryUsageSetting = Objects.requireNonNull(memoryUsageSetting); | ||
this.cfg = cfg; | ||
} | ||
|
||
public void exportToFile(RecordSheetBook book, PageFormat pageFormat, String fileName) | ||
throws IOException, ConfigurationException, TranscoderException, SAXException { | ||
PDFMergerUtility merger = new PDFMergerUtility(); | ||
merger.setDestinationFileName(fileName); | ||
|
||
Map<Integer, List<String>> bookmarkNames = new HashMap<>(); | ||
addSheetsToPdf(merger, book, pageFormat, bookmarkNames); | ||
|
||
// Load newly created document, add an outline, then write back to the file. | ||
File file = new File(fileName); | ||
try (PDDocument doc = PDDocument.load(file)) { | ||
addBookmarksToDocument(bookmarkNames, doc); | ||
doc.save(file); | ||
} | ||
} | ||
|
||
private void addBookmarksToDocument(Map<Integer, List<String>> bookmarkNames, PDDocument doc) { | ||
PDDocumentOutline outline = new PDDocumentOutline(); | ||
doc.getDocumentCatalog().setDocumentOutline(outline); | ||
for (Map.Entry<Integer, List<String>> entry : bookmarkNames.entrySet()) { | ||
for (String name : entry.getValue()) { | ||
PDOutlineItem bookmark = new PDOutlineItem(); | ||
bookmark.setDestination(doc.getPage(entry.getKey())); | ||
bookmark.setTitle(name); | ||
outline.addLast(bookmark); | ||
} | ||
} | ||
|
||
outline.openNode(); | ||
} | ||
|
||
private void addSheetsToPdf(PDFMergerUtility merger, RecordSheetBook book, PageFormat pageFormat, | ||
Map<Integer, List<String>> bookmarkNames) | ||
throws TranscoderException, SAXException, IOException, ConfigurationException { | ||
List<PrintRecordSheet> sheets = book.getSheets(); | ||
for (PrintRecordSheet rs : sheets) { | ||
bookmarkNames.put(rs.getFirstPage(), rs.getBookmarkNames()); | ||
for (int i = 0; i < rs.getPageCount(); i++) { | ||
merger.addSource(rs.exportPDF(i, pageFormat, getOrCreateConfiguration(rs))); | ||
} | ||
} | ||
|
||
merger.mergeDocuments(memoryUsageSetting); | ||
} | ||
|
||
private Configuration getOrCreateConfiguration(PrintRecordSheet sheet) | ||
throws ConfigurationException, SAXException, IOException { | ||
if (cfg != null) { | ||
return cfg; | ||
} else { | ||
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); | ||
return cfgBuilder.build(sheet.getClass().getResourceAsStream("fop-config.xml")); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package megameklab.com.printing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import megamek.common.Entity; | ||
|
||
public class RecordSheetBook { | ||
private final List<PrintRecordSheet> sheets = new ArrayList<>(); | ||
private final List<Entity> unprintable = new ArrayList<>(); | ||
|
||
public List<PrintRecordSheet> getSheets() { | ||
return Collections.unmodifiableList(sheets); | ||
} | ||
|
||
public void addSheet(PrintRecordSheet recordSheet) { | ||
sheets.add(Objects.requireNonNull(recordSheet)); | ||
} | ||
|
||
public boolean hasUnprintableEntities() { | ||
return !unprintable.isEmpty(); | ||
} | ||
|
||
public List<Entity> getUnprintableEntities() { | ||
return Collections.unmodifiableList(unprintable); | ||
} | ||
|
||
public void addUnprintableEntity(Entity entity) { | ||
unprintable.add(Objects.requireNonNull(entity)); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
src/megameklab/com/printing/RecordSheetBookBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package megameklab.com.printing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import megamek.common.*; | ||
import megameklab.com.util.UnitUtil; | ||
|
||
public class RecordSheetBookBuilder { | ||
private final List<Entity> entities = new ArrayList<>(); | ||
private boolean isSinglePrint; | ||
private RecordSheetOptions recordSheetOptions; | ||
|
||
public RecordSheetBookBuilder setSinglePrint(boolean singlePrint) { | ||
isSinglePrint = singlePrint; | ||
return this; | ||
} | ||
|
||
public RecordSheetBookBuilder setRecordSheetOptions(RecordSheetOptions options) { | ||
this.recordSheetOptions = options; | ||
return this; | ||
} | ||
|
||
public RecordSheetBookBuilder addEntity(Entity entity) { | ||
entities.add(entity); | ||
return this; | ||
} | ||
|
||
public RecordSheetBookBuilder addEntities(Collection<Entity> entities) { | ||
this.entities.addAll(entities); | ||
return this; | ||
} | ||
|
||
public RecordSheetBook build() { | ||
final boolean singlePrint = this.isSinglePrint; | ||
final RecordSheetOptions options = (this.recordSheetOptions != null) | ||
? this.recordSheetOptions : new RecordSheetOptions(); | ||
|
||
RecordSheetBook book = new RecordSheetBook(); | ||
|
||
List<Infantry> infList = new ArrayList<>(); | ||
List<BattleArmor> baList = new ArrayList<>(); | ||
List<Protomech> protoList = new ArrayList<>(); | ||
Tank tank1 = null; | ||
|
||
int pageCount = 0; | ||
for (Entity unit : entities) { | ||
if (unit instanceof Mech) { | ||
UnitUtil.removeOneShotAmmo(unit); | ||
UnitUtil.expandUnitMounts((Mech) unit); | ||
book.addSheet(new PrintMech((Mech) unit, pageCount++, options)); | ||
} else if ((unit instanceof Tank) && isSingleTankRecordSheet(unit)) { | ||
book.addSheet(new PrintTank((Tank) unit, pageCount++, options)); | ||
} else if (unit instanceof Tank) { | ||
if (singlePrint || options.showReferenceCharts()) { | ||
book.addSheet(new PrintCompositeTankSheet((Tank) unit, null, pageCount++, options)); | ||
} else if (null != tank1) { | ||
book.addSheet(new PrintCompositeTankSheet(tank1, (Tank) unit, pageCount++, options)); | ||
tank1 = null; | ||
} else { | ||
tank1 = (Tank) unit; | ||
} | ||
} else if (unit.hasETypeFlag(Entity.ETYPE_AERO)) { | ||
if (unit instanceof Jumpship) { | ||
PrintCapitalShip pcs = new PrintCapitalShip((Jumpship) unit, pageCount, options); | ||
pageCount += pcs.getPageCount(); | ||
book.addSheet(pcs); | ||
} else if (unit instanceof Dropship) { | ||
PrintDropship pds = new PrintDropship((Aero) unit, pageCount, options); | ||
pageCount += pds.getPageCount(); | ||
book.addSheet(pds); | ||
} else { | ||
book.addSheet(new PrintAero((Aero) unit, pageCount++, options)); | ||
} | ||
} else if (unit instanceof BattleArmor) { | ||
baList.add((BattleArmor) unit); | ||
if (singlePrint || baList.size() > 4) { | ||
PrintRecordSheet prs = new PrintSmallUnitSheet(baList, pageCount, options); | ||
pageCount += prs.getPageCount(); | ||
book.addSheet(prs); | ||
baList = new ArrayList<>(); | ||
} | ||
} else if (unit instanceof Infantry) { | ||
infList.add((Infantry) unit); | ||
if (singlePrint || infList.size() > (options.showReferenceCharts() ? 2 : 3)) { | ||
PrintRecordSheet prs = new PrintSmallUnitSheet(infList, pageCount, options); | ||
pageCount += prs.getPageCount(); | ||
book.addSheet(prs); | ||
infList = new ArrayList<>(); | ||
} | ||
} else if (unit instanceof Protomech) { | ||
protoList.add((Protomech) unit); | ||
if (singlePrint || protoList.size() > 4) { | ||
PrintRecordSheet prs = new PrintSmallUnitSheet(protoList, pageCount, options); | ||
pageCount += prs.getPageCount(); | ||
book.addSheet(prs); | ||
protoList = new ArrayList<>(); | ||
} | ||
} else if (unit != null) { | ||
book.addUnprintableEntity(unit); | ||
} | ||
} | ||
|
||
if (null != tank1) { | ||
book.addSheet(new PrintCompositeTankSheet(tank1, null, pageCount++)); | ||
} | ||
if (baList.size() > 0) { | ||
book.addSheet(new PrintSmallUnitSheet(baList, pageCount++)); | ||
} | ||
if (infList.size() > 0) { | ||
book.addSheet(new PrintSmallUnitSheet(infList, pageCount++)); | ||
} | ||
if (protoList.size() > 0) { | ||
book.addSheet(new PrintSmallUnitSheet(protoList, pageCount)); | ||
} | ||
|
||
return book; | ||
} | ||
|
||
private static boolean isSingleTankRecordSheet(Entity unit) { | ||
return (unit instanceof Tank) | ||
&& ((unit.getMovementMode() == EntityMovementMode.NAVAL) | ||
|| (unit.getMovementMode() == EntityMovementMode.SUBMARINE) | ||
|| (unit.getMovementMode() == EntityMovementMode.HYDROFOIL)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.