Search in sources :

Example 36 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class CIDType2DescriptorHelper method extractFontFile.

@Override
public PDStream extractFontFile(PDFontDescriptor fontDescriptor) {
    PDStream ff2 = fontDescriptor.getFontFile2();
    if (ff2 != null) {
        // Stream validation should be done by the StreamValidateHelper. Process font specific check
        COSStream stream = ff2.getCOSObject();
        if (stream == null) {
            this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": The FontFile is missing"));
            this.fContainer.notEmbedded();
        }
    }
    checkCIDSet(fontDescriptor);
    return ff2;
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) PDStream(org.apache.pdfbox.pdmodel.common.PDStream) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 37 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class FontDescriptorHelper method checkFontFileMetaData.

/**
 * Type0, Type1 and TrueType FontValidator call this method to check the FontFile meta data.
 *
 * @param fontDescriptor
 *            The FontDescriptor which contains the FontFile stream
 * @param fontFile
 *            The font file stream to check
 */
protected void checkFontFileMetaData(PDFontDescriptor fontDescriptor, PDStream fontFile) {
    try {
        PDMetadata metadata = fontFile.getMetadata();
        if (metadata != null) {
            // Filters are forbidden in a XMP stream
            if (metadata.getFilters() != null && !metadata.getFilters().isEmpty()) {
                this.fContainer.push(new ValidationError(ERROR_SYNTAX_STREAM_INVALID_FILTER, this.font.getName() + ": Filter specified in font file metadata dictionnary"));
                return;
            }
            byte[] mdAsBytes = getMetaDataStreamAsBytes(metadata);
            try {
                DomXmpParser xmpBuilder = new DomXmpParser();
                XMPMetadata xmpMeta = xmpBuilder.parse(mdAsBytes);
                FontMetaDataValidation fontMDval = new FontMetaDataValidation();
                List<ValidationError> ve = new ArrayList<>();
                fontMDval.analyseFontName(xmpMeta, fontDescriptor, ve);
                fontMDval.analyseRights(xmpMeta, fontDescriptor, ve);
                this.fContainer.push(ve);
            } catch (XmpParsingException e) {
                if (e.getErrorType() == ErrorType.NoValueType) {
                    this.fContainer.push(new ValidationError(ERROR_METADATA_UNKNOWN_VALUETYPE, e.getMessage(), e));
                } else if (e.getErrorType() == ErrorType.XpacketBadEnd) {
                    this.fContainer.push(new ValidationError(ERROR_METADATA_FORMAT_XPACKET, this.font.getName() + ": Unable to parse font metadata due to : " + e.getMessage(), e));
                } else {
                    this.fContainer.push(new ValidationError(ERROR_METADATA_FORMAT, e.getMessage(), e));
                }
            }
        }
    } catch (IllegalStateException e) {
        this.fContainer.push(new ValidationError(ERROR_METADATA_FORMAT_UNKOWN, this.font.getName() + ": The Metadata entry doesn't reference a stream object", e));
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) XMPMetadata(org.apache.xmpbox.XMPMetadata) FontMetaDataValidation(org.apache.pdfbox.preflight.font.util.FontMetaDataValidation) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) ArrayList(java.util.ArrayList) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 38 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class Type1DescriptorHelper method extractFontFile.

@Override
public PDStream extractFontFile(PDFontDescriptor fontDescriptor) {
    PDStream ff1 = fontDescriptor.getFontFile();
    PDStream ff3 = fontDescriptor.getFontFile3();
    if (ff1 != null) {
        COSStream stream = ff1.getCOSObject();
        if (stream == null) {
            this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": The FontFile is missing"));
            this.fContainer.notEmbedded();
            return null;
        }
        boolean hasLength1 = stream.getInt(COSName.LENGTH1) > 0;
        boolean hasLength2 = stream.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH2)) > 0;
        boolean hasLength3 = stream.getInt(COSName.getPDFName(FONT_DICTIONARY_KEY_LENGTH3)) >= 0;
        if (!(hasLength1 && hasLength2 && hasLength3)) {
            this.fContainer.push(new ValidationError(ERROR_FONTS_FONT_FILEX_INVALID, fontDescriptor.getFontName() + ": The FontFile is invalid"));
            return null;
        }
        return ff1;
    } else {
        return ff3;
    }
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) PDStream(org.apache.pdfbox.pdmodel.common.PDStream) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 39 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class FontMetaDataValidation method analyseFontName.

/**
 * Value of the dc:title must be the same as the FontName in the font descriptor.
 *
 * @param metadata
 *            XMPMetaData of the Font File Stream
 * @param fontDesc
 *            The FontDescriptor dictionary
 * @param ve
 *            the list of validation error to update if the validation fails
 */
public boolean analyseFontName(XMPMetadata metadata, PDFontDescriptor fontDesc, List<ValidationError> ve) {
    String fontName = fontDesc.getFontName();
    String noSubSetName = fontName;
    if (FontDescriptorHelper.isSubSet(fontName)) {
        noSubSetName = fontName.split("\\+")[1];
    }
    DublinCoreSchema dc = metadata.getDublinCoreSchema();
    if (dc != null && dc.getTitleProperty() != null) {
        String defaultTitle = dc.getTitle("x-default");
        if (defaultTitle != null) {
            if (!defaultTitle.equals(fontName) && (noSubSetName != null && !defaultTitle.equals(noSubSetName))) {
                StringBuilder sb = new StringBuilder(80);
                sb.append("FontName").append(" present in the FontDescriptor dictionary doesn't match with XMP information dc:title of the Font File Stream.");
                ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_MISMATCH, sb.toString()));
                return false;
            }
            // --- default value is the right one
            return true;
        } else {
            Iterator<AbstractField> it = dc.getTitleProperty().getContainer().getAllProperties().iterator();
            boolean empty = true;
            while (it.hasNext()) {
                empty = false;
                AbstractField tmp = it.next();
                if (tmp instanceof TextType) {
                    String val = ((TextType) tmp).getStringValue();
                    if (val.equals(fontName) || val.equals(noSubSetName)) {
                        // value found, return
                        return true;
                    }
                }
            }
            // title doesn't match, it is an error.
            StringBuilder sb = new StringBuilder(80);
            sb.append("FontName");
            if (empty) {
                sb.append(" present in the FontDescriptor dictionary can't be found in XMP information the Font File Stream.");
                ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_PROPERTY_MISSING, sb.toString()));
            } else {
                sb.append(" present in the FontDescriptor dictionary doesn't match with XMP information dc:title of the Font File Stream.");
                ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_MISMATCH, sb.toString()));
            }
            return false;
        }
    }
    return true;
}
Also used : AbstractField(org.apache.xmpbox.type.AbstractField) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) TextType(org.apache.xmpbox.type.TextType)

Example 40 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class DeviceColorSpaceHelper method processIndexedColorSpace.

/**
 * Indexed color space is authorized only if the BaseColorSpace is a DeviceXXX color space. In all other cases the
 * given list is updated with a ValidationError (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns
 * false.
 */
@Override
protected void processIndexedColorSpace(PDColorSpace colorSpace) {
    PDIndexed indexed = (PDIndexed) colorSpace;
    PDColorSpace baseColorSpace = indexed.getBaseColorSpace();
    ColorSpaces colorSpaces = ColorSpaces.valueOf(baseColorSpace.getName());
    switch(colorSpaces) {
        case Indexed:
        case I:
        case Pattern:
            context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, colorSpaces.getLabel() + " ColorSpace is forbidden"));
            break;
        default:
            processAllColorSpace(baseColorSpace);
    }
}
Also used : PDIndexed(org.apache.pdfbox.pdmodel.graphics.color.PDIndexed) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Aggregations

ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)108 COSBase (org.apache.pdfbox.cos.COSBase)36 COSDictionary (org.apache.pdfbox.cos.COSDictionary)28 IOException (java.io.IOException)27 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSObject (org.apache.pdfbox.cos.COSObject)13 COSArray (org.apache.pdfbox.cos.COSArray)10 COSStream (org.apache.pdfbox.cos.COSStream)10 COSString (org.apache.pdfbox.cos.COSString)8 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)8 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)8 COSName (org.apache.pdfbox.cos.COSName)7 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)7 PreflightPath (org.apache.pdfbox.preflight.PreflightPath)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)5 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)5 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)5 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)4