zxing/zxing · error · IllegalArgumentException

Can only encode PDF_417, but got {format}

Error message

Can only encode PDF_417, but got {format}

What it means

Error "Can only encode PDF_417, but got {format}" thrown in zxing/zxing.

Source

Thrown at core/src/main/java/com/google/zxing/pdf417/PDF417Writer.java:54

  /**
   * default white space (margin) around the code
   */
  private static final int WHITE_SPACE = 30;

  /**
   * default error correction level
   */
  private static final int DEFAULT_ERROR_CORRECTION_LEVEL = 2;

  @Override
  public BitMatrix encode(String contents,
                          BarcodeFormat format,
                          int width,
                          int height,
                          Map<EncodeHintType,?> hints) throws WriterException {
    if (format != BarcodeFormat.PDF_417) {
      throw new IllegalArgumentException("Can only encode PDF_417, but got " + format);
    }

    PDF417 encoder = new PDF417();
    int margin = WHITE_SPACE;
    int errorCorrectionLevel = DEFAULT_ERROR_CORRECTION_LEVEL;
    boolean autoECI = false;

    if (hints != null) {
      if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) {
        encoder.setCompact(Boolean.parseBoolean(hints.get(EncodeHintType.PDF417_COMPACT).toString()));
      }
      if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) {
        encoder.setCompaction(Compaction.valueOf(hints.get(EncodeHintType.PDF417_COMPACTION).toString()));
      }
      if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) {
        Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS);
        encoder.setDimensions(dimensions.getMaxCols(),
                              dimensions.getMinCols(),

View on GitHub (pinned to 19aa2d8254)

Solutions

  1. Call encode() with BarcodeFormat.PDF_417.
  2. Use a different Writer implementation for the requested barcode format.

When it happens

Trigger: Thrown at core/src/main/java/com/google/zxing/pdf417/PDF417Writer.java:54 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zxing/zxing@19aa2d8254 (2026-08-14). Data as JSON: /api/errors/a03e75da54c4b2c6. Report an issue: GitHub.