Skip to content
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

Fix PDF export #10361

Merged
merged 13 commits into from
Oct 3, 2023
45 changes: 35 additions & 10 deletions src/main/java/org/jabref/logic/exporter/XmpPdfExporter.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.jabref.logic.exporter;

import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.io.IOException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intendation off?

import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.logic.xmp.XmpUtilWriter;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.logic.xmp.XmpUtilWriter;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;

public class XmpPdfExporter extends Exporter {

Expand All @@ -26,8 +31,28 @@ public void export(BibDatabaseContext databaseContext, Path pdfFile, List<BibEnt
Objects.requireNonNull(pdfFile);
Objects.requireNonNull(entries);

if (pdfFile.toString().endsWith(".pdf")) {
Path filePath = pdfFile.toAbsolutePath();

if (!pdfFile.toString().endsWith(".pdf")) {
throw new IllegalArgumentException("Invalid PDF file extension");
}
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);

PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.newLineAtOffset(25, 500);
contentStream.showText("This PDF was created by JabRef. It demonstrates the embedding of BibTeX data in PDF files. Please open the file attachments view of your PDF viewer to see the attached files. Note that the normal usage is to embed the BibTeX data in an existing PDF.");
contentStream.endText();

document.save(filePath.toString());
document.close();

new XmpUtilWriter(xmpPreferences).writeXmp(pdfFile, entries, databaseContext.getDatabase());

} catch (IOException e) {
throw new Exception("Error creating PDF", e);
}
}
}
Loading