Article View: fr.comp.sys.mac
Article #40[Solution] Re: Créer un formulaire PDF
From: Benoît L.
Date: Wed, 11 Nov 2026 13:47
Date: Wed, 11 Nov 2026 13:47
149 lines
6242 bytes
6242 bytes
Benoît L. <benoit@leraillez.com.invalid> wrote:
> Hugh !
>
> Je voudrai savoir s’il est possible d’ajouter des cases à cocher sur un
> pdf. Le but est de faire un document avec des photos et le lecteur peut
> cocher « J’aime, J’aime pas, C’est nul, À refaire »
Bon, après quelques heures d’essais à droite et à gauche, j’ai pausé la
question à Claude et après avoir demandé quelques petites corrections c’est
plus que pas mal.
Pour ceux que ça intéresse le code .py (à suivre sur fcsmp). Il y a aussi
du .lu que je n’ai pas encore demandé.
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib.colors import HexColor, white
from reportlab.lib.utils import ImageReader
import glob, os
N_PHOTOS = 6 # nombre de photos par défaut (si aucune image
fournie)
PHOTO_DIR = "photos" # dossier optionnel contenant les vraies photos
W, H = A4
INK = HexColor("#1f2937")
MUTED = HexColor("#6b7280")
ACCENT = HexColor("#2563eb")
LINE = HexColor("#d1d5db")
BG = HexColor("#f3f4f6")
images = sorted(
glob.glob(os.path.join(PHOTO_DIR, "*.jpg"))
+ glob.glob(os.path.join(PHOTO_DIR, "*.jpeg"))
+ glob.glob(os.path.join(PHOTO_DIR, "*.png"))
)
n = len(images) if images else N_PHOTOS
pages = n # une photo par page
c = canvas.Canvas("formulaire_photos.pdf", pagesize=A4)
c.setTitle("Donne ton avis sur les photos")
form = c.acroForm
OPTIONS = [("jaime", "J'aime"), ("bof", "Bof"), ("jaimepas", "J'aime pas")]
MARGIN = 40
HEADER_H = 56
BOX_W = W - 2 * MARGIN
BOX_H = round(H * 2 / 3) # deux tiers de la hauteur de page
def header(i):
c.setFillColor(ACCENT)
c.rect(0, H - HEADER_H, W, HEADER_H, stroke=0, fill=1)
c.setFillColor(white)
c.setFont("Helvetica-Bold", 20)
c.drawString(MARGIN, H - 28, "Donne ton avis sur les photos")
c.setFont("Helvetica", 10)
c.drawString(MARGIN, H - 45, f"Photo {i + 1} sur {pages}")
if i == 0:
c.setFont("Helvetica", 9)
c.drawString(W - 250, H - 20, "Ton prénom (facultatif) :")
form.textfield(name="prenom", x=W - 250, y=H - 49, width=210,
height=22,
borderColor=white, fillColor=white, textColor=INK,
fontSize=10, borderWidth=1, forceBorder=True)
def footer():
c.setFillColor(MUTED)
c.setFont("Helvetica", 8)
c.drawString(MARGIN, 22, "Coche une seule réponse par photo. Les autres
champs sont facultatifs.")
for i in range(n):
header(i)
# cadre photo : 2/3 de la hauteur de la page
box_top = H - HEADER_H - 8
box_y = box_top - BOX_H
c.setFillColor(BG)
c.setStrokeColor(LINE)
c.rect(MARGIN, box_y, BOX_W, BOX_H, stroke=1, fill=1)
if images:
img = ImageReader(images[i])
iw, ih = img.getSize()
scale = min(BOX_W / iw, BOX_H / ih)
dw, dh = iw * scale, ih * scale
c.drawImage(img, MARGIN + (BOX_W - dw) / 2, box_y + (BOX_H - dh) /
2,
dw, dh, preserveAspectRatio=True)
else:
c.setFillColor(MUTED)
c.setFont("Helvetica", 12)
c.drawCentredString(MARGIN + BOX_W / 2, box_y + BOX_H / 2, f"[
Emplacement photo {i + 1} ]")
# choix, sous la photo
y = box_y - 28
c.setFillColor(INK)
c.setFont("Helvetica-Bold", 11)
c.drawString(MARGIN, y + 2, "Ton avis :")
for k, (val, label) in enumerate(OPTIONS):
ox = MARGIN + 85 + k * 120
form.radio(name=f"avis_{i + 1}", tooltip=f"Photo {i + 1} :
{label}",
value=val, selected=False, x=ox, y=y - 2,
size=16,
buttonStyle="circle", borderColor=ACCENT,
fillColor=white,
textColor=ACCENT, forceBorder=True)
c.setFillColor(INK)
c.setFont("Helvetica", 11)
c.drawString(ox + 24, y + 2, label)
# case à cocher indépendante
y2 = y - 28
form.checkbox(name=f"texte_a_revoir_{i + 1}", tooltip=f"Photo {i + 1} :
texte à revoir",
x=MARGIN + 85, y=y2 - 2, size=16, buttonStyle="check",
borderColor=ACCENT, fillColor=white, textColor=ACCENT,
forceBorder=True)
c.setFillColor(INK)
c.setFont("Helvetica", 11)
c.drawString(MARGIN + 85 + 24, y2 + 2, "Texte à revoir")
# commentaire
y3 = y2 - 22
c.setFillColor(INK)
c.setFont("Helvetica", 10)
c.drawString(MARGIN, y3, "Commentaire :")
field_h = y3 - 8 - 40
form.textfield(name=f"commentaire_{i + 1}", x=MARGIN, y=40,
width=BOX_W,
height=field_h, borderColor=LINE,
fillColor=white, textColor=INK,
fontSize=9, borderWidth=1, forceBorder=True,
fieldFlags="multiline")
footer()
c.showPage()
c.save()
print("OK", n, "photos,", pages, "pages; box_h", BOX_H, "box_y", box_y,
"comment h", field_h)
--
Et Hop ! ©®™
Technical Details
Message-ID:
<11d1rlh$ci$1@rasp.pasdenom.info>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!pasdenom.info!.POSTED.abordeaux-257-1-9-100.w90-60.abo.wanadoo.fr!not-for-mailReferences:
<11cfq88$38g$1@rasp.pasdenom.info>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!pasdenom.info!.POSTED.abordeaux-257-1-9-100.w90-60.abo.wanadoo.fr!not-for-mail From: Benoît L. <benoit@leraillez.com.invalid> Newsgroups: fr.comp.sys.mac Subject: [Solution] Re: Créer un formulaire PDF Date: Wed, 11 Nov 2026 13:47:29 -0000 (UTC) Organization: <http://pasdenom.info/news.html> Message-ID: <11d1rlh$ci$1@rasp.pasdenom.info> References: <11cfq88$38g$1@rasp.pasdenom.info> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Wed, 11 Nov 2026 13:47:29 -0000 (UTC) Injection-Info: rasp.pasdenom.info; posting-account="benoit@usenet"; posting-host="abordeaux-257-1-9-100.w90-60.abo.wanadoo.fr:90.60.108.100"; logging-data="402"; mail-complaints-to="abuse@pasdenom.info" User-Agent: NewsTap/5.6.3 (iPad) Cancel-Lock: sha1:xuaXz1NouilEklzFS48MSEv1ckk= sha1:ciXIKrdChRdgayLqQc5pQvBcUdY= sha256:oSnFDfE/3lWe2yWn9/krApEJUlIEZgx/Ulb+G5yloq4= sha1:9X2xFvLghPg4Bvj20sFcuRSaFe0= sha256:qJhUfqEM28/wprlyIRB6AvpJwhXmHjQBrrpn3gHW5YI= X-No-Archive: Yes