import tempfile
import subprocess
import shutil
from docassemble.base.error import DAError
#from docassemble.base.logger import logmessage

def pdf_to_pdfa(filename):
    outfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
    directory = tempfile.mkdtemp()
    commands = ['gs', '-dPDFA', '-dBATCH', '-dNOPAUSE', '-sProcessColorModel=DeviceCMYK', '-sDEVICE=pdfwrite', '-sPDFACompatibilityPolicy=1', '-sOutputFile=' + outfile.name, filename]
    try:
        output = subprocess.check_output(commands, cwd=directory, stderr=subprocess.STDOUT).decode()
    except subprocess.CalledProcessError as err:
        output = err.output.decode()
        raise DAError("pdf_to_pdfa: error running ghostscript.  " + output)
    shutil.move(outfile.name, filename)

import tempfile
import subprocess
import shutil
from docassemble.base.error import DAError
#from docassemble.base.logger import logmessage

def pdf_to_pdfa(filename):
    outfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
    directory = tempfile.mkdtemp()
    commands = ['gs', '-dPDFA', '-dBATCH', '-dNOPAUSE', '-sColorConversionStrategy=UseDeviceIndependentColor', '-sProcessColorModel=DeviceCMYK', '-sDEVICE=pdfwrite', '-sPDFACompatibilityPolicy=1', '-sOutputFile=' + outfile.name, filename]
    try:
        output = subprocess.check_output(commands, cwd=directory, stderr=subprocess.STDOUT).decode()
    except subprocess.CalledProcessError as err:
        output = err.output.decode()
        raise DAError("pdf_to_pdfa: error running ghostscript.  " + output)
    shutil.move(outfile.name, filename)

Make ghostscript handle PDF/A colorspace correctly
Make ghostscript handle PDF/A colorspace correctly

Previously, if you tried to verify files generated by pdf_to_pdfa in a
verifier (like https://tools.pdfforge.org/validate-pdfa), you would get
errors relating to the lack of OutputIntent (6.2.3). The info in
https://stackoverflow.com/a/56459053/11416267 and
https://www.ghostscript.com/doc/current/VectorDevices.htm#PDFA suggested
that `-sColorConversionStrategy=UseDeviceIndependentColor` be added,
which corrects those validator errors, and verifies output PDFs as PDF/A-1b.
