Using custom font with flying saucer gives corrupt PDF - flying-saucer

I have a working PDF generated with Flying Saucer. When I now however want to add custom fonts, the generated PDF becomes corrupt. Instead of downloading the file, the browser shows all kind of weird symbols.
The custom font was added by adding the ttf font files on the classpath and calling addFont:
renderer.getFontResolver().addFont("/fonts/Montserrat-Regular.ttf", BaseFont.IDENTITY_H, true);
renderer.getFontResolver().addFont("/fonts/Montserrat-Italic.ttf", BaseFont.IDENTITY_H, true);
renderer.getFontResolver().addFont("/fonts/Montserrat-Bold.ttf", BaseFont.IDENTITY_H, true);
renderer.getFontResolver().addFont("/fonts/Montserrat-BoldItalic.ttf", BaseFont.IDENTITY_H, true);
And specifying the font in CSS:
html {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
}

The problem was due to the response header being set after the PDF was generated into the outputstream of the response.
My controller was something like this:
#GetMapping("/{id}/download-pdf")
#Secured(Roles.ADMIN)
public void downloadPDFResource(#PathVariable("id") EntityId entityId,
HttpServletRequest request, HttpServletResponse response,
Locale locale) throws IOException {
byte[] pdf = pdfService.generatePdf("details-pdf", context);
response.getOutputStream().write(pdf);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition",
"attachment; filename=" + "document.pdf");
response.getOutputStream().flush();
}
The fix was to move the setting of the content type and header to the top of the method:
#GetMapping("/{id}/download-pdf")
#Secured(Roles.ADMIN)
public void downloadPDFResource(#PathVariable("id") EntityId entityId,
HttpServletRequest request, HttpServletResponse response,
Locale locale) throws IOException {
response.setContentType("application/pdf");
response.addHeader("Content-Disposition",
"attachment; filename=" + "document.pdf");
byte[] pdf = pdfService.generatePdf("details-pdf", context);
response.getOutputStream().write(pdf);
response.getOutputStream().flush();
}

Related

JSF PDF not downloadable while returned from Servlet [duplicate]

This question already has an answer here:
Internet Explorer 9 does not use file name for inline attachments
(1 answer)
Closed 3 years ago.
I have a form that once completed opens a new tab and calls my bean method annonceReturn().
This method sends back a pdf file. The problem is that as I open a new tab with _blank, the URL ends with .xhtml. Even the filename is displayed as being in my exemple "list.xhtml" (the last part of the URL). The problem is that I can't download this file because it's not considerated as a pdf file.
This is my xhtml file :
<h:form id="form">
<p:commandButton id="envoiRetour" onclick="this.form.target = '_blank';"
actionListener="#{returnCtrl.announceReturn()}"
value="Open PDF in new tab"
ajax="false" />
</h:form>
This is the returnCtrl.annonceReturn() method :
public void announceReturn() throws MalformedURLException, FileNotFoundException, DocumentException, BadElementException, IOException, InterruptedException {
String referenceAnnouncement = "C:/Users/path_to_my_pdf_file.pdf";
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(referenceAnnouncement), 10240);
response.reset();
response.setHeader("Content-type", "application/pdf");
response.setContentLength((int)new File(referenceAnnouncement).length());
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Content-disposition", "inline; filename=" + "file.pdf");
response.setHeader("pragma", "public");
output = new BufferedOutputStream(response.getOutputStream(), 10240);
byte[] buffer = new byte[10240];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
} finally {
output.close();
input.close();
}
}
How can I do to open this PDF in a new tab and be able to download it ?
When I try to download it, it says there is a network error (and it tries to save it as xhtml file).
EDIT : this is the question that helped me : How to open a PDF file in a new tab
EDIT 2 : the problem is not that the PDF doesn't show. The problem is that it shows in the new tab but when I try to download it, the explorer wants to save it as an XHTML file.
EDIT 3 : as mentionned here -> Open PDF in new tab, saving file gives wrong file name
it seems the filename is ignored if the disposition is not "attachment"... So I think I need to think about another way to do it.
Thanks for your time.
Try 'attachment' instead of 'inline' to force the browser saving the file (instead of trying to open with the associated plugin - if installed)
response.setHeader("Content-disposition", "attachment; filename=" + "file.pdf");
Hope it helps.
Beppe
Here is my code, hope it helps.
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
File temp = File("./path/abc.pdf");
temp.deleteOnExit();
ec.responseReset();
ec.setResponseContentType("application/pdf");
ec.setResponseContentLength((int)temp.length());
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + temp.getName() + "\""); //inline;
OutputStream output = ec.getResponseOutputStream();
Files.copy(temp.toPath(), output);
fc.responseComplete();
I have tested in my project, here is the solution:
1 - Create button
<h:commandButton
onclick="this.form.target='_blank'"
id="cmdOpenPDF"
action="#{bean.openPDF(bean.code)}"
value="New PDF">
</h:commandButton>
2 - The bean has a function with a redirect link to PDF. This sample the pdf in the root folder.
public void openPDF(String code) throws Exception {
String filePdfName ="";
try {
//Make sure that this file in root
File pdf = new File("sample.pdf");
filePdfName = pdf.getName();
} catch (Exception ex) {
ex.printStackTrace();
}
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/" + filePdfName);
}

Apache PDFBox and PDF/A-3

Is it possible to use Apache PDFBox to process PDF/A-3 documents? (Especially for changing field values?)
The PDFBox 1.8 Cookbook says that it is possible to create PDF/A-1 documents with pdfaid.setPart(1);
Can I apply pdfaid.setPart(3) for a PDF/A-3 document?
If not: Is it possible to read in a PDF/A-3 document, change some field values and safe it by what I have not need for >creation/conversion to PDF/A-3< but the document is still PDF/A-3?
How to create a PDF/A {2,3} - {B, U, A) valid: In this example I convert the PDF to Image, then I create a valid PDF / Ax-y with the image. PDFBOX2.0x
public static void main(String[] args) throws IOException, TransformerException
{
String resultFile = "result/PDFA-x.PDF";
FileInputStream in = new FileInputStream("src/PDFOrigin.PDF");
PDDocument doc = new PDDocument();
try
{
PDPage page = new PDPage();
doc.addPage(page);
doc.setVersion(1.7f);
/*
// A PDF/A file needs to have the font embedded if the font is used for text rendering
// in rendering modes other than text rendering mode 3.
//
// This requirement includes the PDF standard fonts, so don't use their static PDFType1Font classes such as
// PDFType1Font.HELVETICA.
//
// As there are many different font licenses it is up to the developer to check if the license terms for the
// font loaded allows embedding in the PDF.
String fontfile = "/org/apache/pdfbox/resources/ttf/ArialMT.ttf";
PDFont font = PDType0Font.load(doc, new File(fontfile));
if (!font.isEmbedded())
{
throw new IllegalStateException("PDF/A compliance requires that all fonts used for"
+ " text rendering in rendering modes other than rendering mode 3 are embedded.");
}
*/
PDPageContentStream contents = new PDPageContentStream(doc, page);
try
{
PDDocument docSource = PDDocument.load(in);
PDFRenderer pdfRenderer = new PDFRenderer(docSource);
int numPage = 0;
BufferedImage imagePage = pdfRenderer.renderImageWithDPI(numPage, 200);
PDImageXObject pdfXOImage = LosslessFactory.createFromImage(doc, imagePage);
contents.drawImage(pdfXOImage, 0,0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
contents.close();
}catch (Exception e) {
// TODO: handle exception
}
// add XMP metadata
XMPMetadata xmp = XMPMetadata.createXMPMetadata();
PDDocumentCatalog catalogue = doc.getDocumentCatalog();
Calendar cal = Calendar.getInstance();
try
{
DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
// dc.setTitle(file);
dc.addCreator("My APPLICATION Creator");
dc.addDate(cal);
PDFAIdentificationSchema id = xmp.createAndAddPFAIdentificationSchema();
id.setPart(3); //value => 2|3
id.setConformance("A"); // value => A|B|U
XmpSerializer serializer = new XmpSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(xmp, baos, true);
PDMetadata metadata = new PDMetadata(doc);
metadata.importXMPMetadata(baos.toByteArray());
catalogue.setMetadata(metadata);
}
catch(BadFieldValueException e)
{
throw new IllegalArgumentException(e);
}
// sRGB output intent
InputStream colorProfile = CreatePDFA.class.getResourceAsStream(
"../../../pdmodel/sRGB.icc");
PDOutputIntent intent = new PDOutputIntent(doc, colorProfile);
intent.setInfo("sRGB IEC61966-2.1");
intent.setOutputCondition("sRGB IEC61966-2.1");
intent.setOutputConditionIdentifier("sRGB IEC61966-2.1");
intent.setRegistryName("http://www.color.org");
catalogue.addOutputIntent(intent);
catalogue.setLanguage("en-US");
PDViewerPreferences pdViewer =new PDViewerPreferences(page.getCOSObject());
pdViewer.setDisplayDocTitle(true);;
catalogue.setViewerPreferences(pdViewer);
PDMarkInfo mark = new PDMarkInfo(); // new PDMarkInfo(page.getCOSObject());
PDStructureTreeRoot treeRoot = new PDStructureTreeRoot();
catalogue.setMarkInfo(mark);
catalogue.setStructureTreeRoot(treeRoot);
catalogue.getMarkInfo().setMarked(true);
PDDocumentInformation info = doc.getDocumentInformation();
info.setCreationDate(cal);
info.setModificationDate(cal);
info.setAuthor("My APPLICATION Author");
info.setProducer("My APPLICATION Producer");;
info.setCreator("My APPLICATION Creator");
info.setTitle("PDF title");
info.setSubject("PDF to PDF/A{2,3}-{A,U,B}");
doc.save(resultFile);
}catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
PDFBox supports that but please be aware that due to the fact that PDFBox is a low level library you have to ensure the conformance yourself i.e. there is no 'Save as PDF/A-3'. You might want to take a look at http://www.mustangproject.org which uses PDFBox to support ZUGFeRD (electronic invoicing) which also needs PDF/A-3.

PDFBox Signature Field not well recognized

I'm going in trouble using PDFBox 2.0.0-RC3 and producing a digital signature field into a PDF.
This is the piece of code i use:
public static void main(String[] args) throws IOException, URISyntaxException
{
PDDocument document;
document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
PDSignatureField signatureBox = new PDSignatureField(acroForm);
signatureBox.setPartialName("ENSGN-MY_SIGNATURE_FIELD-001");
acroForm.getFields().add(signatureBox);
PDAnnotationWidget widget = signatureBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle();
rect.setLowerLeftX(50);
rect.setLowerLeftY(750);
rect.setUpperRightX(250);
rect.setUpperRightY(800);
widget.setRectangle(rect);
page.getAnnotations().add(widget);
try {
document.save("/tmp/mySignatureFieldGEN_PDFBOX.pdf");
document.close();
} catch (Exception io) {
System.out.println(io);
}
}
The code generates a pdf document, i open it with Acrobat Reader and this is the result:
PDF BOX Generated
As you can see, the signature panel on the left is void but the signature field on the left is present and works.
I generate the same PDF with PDFTron. This is the result:
PDF Tron Generated
In this case the signature panel on the left show correctly the presence of the signature field.
I would like to obtain this second case (correct) but i don't understand why PDF Box can do this.
Many thanks
add this:
widget.setPage(page);
This sets the /P entry.
Now the panel on the left appears. How did I get the idea? I got a document with such an empty signature field (from here), and compared it with yours with PDFDebugger.

Import annotations (XFDF) to PDF

I have created a sample program to try to import XFDF to PDF using the Aspose library. The program can be run without exception, but the output PDF does not include any annotations. Any suggestions to solve this problem?
Update - 2014-12-12
I have also sent the issue to Aspose. They can reproduce the same problem and logged a ticket PDFNEWJAVA-34609 in their issue tracking system.
Following is my sample program:
public static void main(String[] args) {
final String ROOT = "C:\\PdfAnnotation\\";
final String sourcePDF = "hackermonthly-issue.pdf";
final String destPDF = "output.pdf";
final String sourceXFDF = "XFDFTest.xfdf";
try
{
// Specify the path of license file
License lic = new License();
lic.setLicense(ROOT + "Aspose.Pdf.lic");
//create an object of PdfAnnotationEditor class
PdfAnnotationEditor editor = new PdfAnnotationEditor();
//bind input PDF file
editor.bindPdf(ROOT + sourcePDF);
//create a file stream for input XFDF file to import annotations
FileInputStream fileStream = new FileInputStream(ROOT + sourceXFDF);
//create an enumeration of all the annotation types which you want to import
//int[] annType = {AnnotationType.Ink };
//import annotations of specified type(s) from XFDF file
//editor.importAnnotationFromXfdf(fileStream, annType);
editor.importAnnotationFromXfdf(fileStream);
//save output pdf file
editor.save(ROOT + destPDF);
} catch (Exception e) {
System.out.println("exception: " + e.getMessage());
}
}

download pdf servlet using content disposition not working fine with IE, chrome and firefox but not in Opera

I m using content disposition to download pdf file from my servlet. My code works fine for chrome, firefox and IE but the problem is when I try to download pdf file using opera, it removes pdf extension and adds htm. The following is my code:
String filename = "abc.pdf";
String filepath = "/pdf/" + filename;
System.out.println("filepath "+filepath);
resp.addHeader("content-disposition", "attachment; filename=" + filename);
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream(filepath);
System.out.println(is.toString());
int read = 0;
byte[] bytes = new byte[1024];
OutputStream os = resp.getOutputStream();
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
System.out.println(read);
os.flush();
os.close();
}catch(Exception ex){
logger.error("Exception occurred while downloading pdf -- "+ex.getMessage());
System.out.println(ex.getStackTrace());
}
You should probably set the content type of the response to application/pdf, to let the browser know that the downloaded file is not a HTML file, but a PDF file.
See ServletResponse.setContentType().