డేటాశీట్ జనరేషన్: ఆన్-డిమాండ్ మరియు బ్యాచ్ PDF సృష్టి
ఈ వ్యాసం ఉత్పత్తి డేటాశీట్లను PDFలుగా ఎలా సృష్టిస్తామో వివరిస్తుంది — ఆన్-డిమాండ్గా (వినియోగదారులు అభ్యర్థించినప్పుడు) మరియు బ్యాచ్లో (ప్రసిద్ధ ఉత్పత్తుల కోసం ముందుగానే సృష్టించబడినవి).
సమస్య: ఉత్పత్తి డేటాశీట్ల సృష్టి
ప్రతి ఉత్పత్తికి వృత్తిపరమైన PDF డేటాశీట్ అవసరం, ఇందులో ఇవి ఉంటాయి:
-
ఉత్పత్తి పేరు మరియు చిత్రాలు
-
వర్గం ప్రకారం సమూహం చేయబడిన సాంకేతిక వివరాలు
-
కంపెనీ బ్రాండింగ్ మరియు సంప్రదింపు సమాచారం
-
ఆప్టిమైజ్ చేయబడిన లేఅవుట్ (ఒకే పేజీ, సమతుల్య నిలువు వరుసలు)
ప్రతి అభ్యర్థనకు PDFలను సృష్టించడం నెమ్మదిగా ఉంటుంది (~2 సెకన్లు ప్రతి PDFకి). మొత్తం 65,000 SKUలను ముందుగానే సృష్టించడం నిల్వ మరియు సమయాన్ని వృథా చేస్తుంది.
పరిష్కారం: హైబ్రిడ్ విధానం
మేము రెండు జనరేషన్ పద్ధతులను ఉపయోగిస్తాము:
ఆన్-డిమాండ్: వినియోగదారు అభ్యర్థించినప్పుడు PDFని సృష్టించండి (మొదటిసారి)
బ్యాచ్: ప్రసిద్ధ ఉత్పత్తుల కోసం PDFలను ముందుగానే సృష్టించండి (ప్రతి రాత్రి)
ఆన్-డిమాండ్ జనరేషన్
URL నిర్మాణం
/ds/<sku>.pdf
/ds/<sku>
రెండు URLలు ఒకే PDFని సృష్టిస్తాయి.
అభ్యర్థన ప్రవాహం
@web.route("/ds/<sku>.pdf")
def datasheet(sku: str):
# Validate SKU
# ... (implementation details omitted)
జనరేషన్ ప్రక్రియ
దశ 1: ఉత్పత్తి డేటాను పొందండి
# Product name
name = expand_sku(sku)
# ... (implementation details omitted)
దశ 2: నిలువు వరుసలను సమతుల్యం చేయండి
పంక్తుల సంఖ్యను సమతుల్యం చేయడానికి ఫీచర్లు 3 నిలువు వరుసలలో పంపిణీ చేయబడతాయి:
# Calculate lines per feature group
all_groups = []
for heading, f_items in features.items():
# ... (implementation details omitted)
దశ 3: చిత్ర ఎత్తులను లెక్కించండి
# Main image height (proportional to aspect ratio)
with Image.open(main_image_path) as img:
main_width, main_height = img.size
# ... (implementation details omitted)
దశ 4: స్పేసర్ను లెక్కించండి
ఫూటర్ను పేజీ దిగువకు నెట్టండి:
available_height = 25.0 # cm
content_height = total_image_height + text_height_cm
spacer_height = max(0, available_height - content_height)
దశ 5: HTMLని రెండర్ చేయండి
html = render_template(
"datasheet.html",
sku=sku,
# ... (implementation details omitted)
దశ 6: PDFని సృష్టించండి
options = {
"page-size": "A4",
"enable-local-file-access": None,
"load-error-handling": "ignore",
"load-media-error-handling": "ignore",
"no-stop-slow-scripts": None
}
pdf_data = pdfkit.from_string(html, options=options)
దశ 7: మొదటి పేజీని మాత్రమే ఉంచండి
reader = PdfReader(BytesIO(pdf_data))
if len(reader.pages) > 1:
writer = PdfWriter()
writer.add_page(reader.pages[0])
output = BytesIO()
writer.write(output)
pdf_data = output.getvalue()
పనితీరు
జనరేషన్ సమయం: ~2 సెకన్లు ప్రతి PDFకి
క్యాషింగ్: క్యాషింగ్ లేదు (ఎల్లప్పుడూ తాజా)
ప్రయోజనం: ఎల్లప్పుడూ తాజా ఉత్పత్తి డేటాతో నవీకరించబడి ఉంటుంది
బ్యాచ్ జనరేషన్
ఉద్దేశ్యం
ఆన్-డిమాండ్ లోడ్ను తగ్గించడానికి ప్రసిద్ధ ఉత్పత్తుల కోసం PDFలను ముందుగానే సృష్టించండి.
స్క్రిప్ట్ స్థానం
scripts/utils/generate_datasheets.py
మార్పులను ట్రాక్ చేయడం
ప్రతి SKUకి సంబంధించిన productdb.json విభాగాలకు మార్పులను మేము ట్రాక్ చేస్తాము:
def get_sku_productdb_hash(sku: str) -> str:
"""Get hash of productdb sections relevant to this SKU."""
with open(PRODUCTDB_PATH, "r") as f:
# ... (implementation details omitted)
ప్రాధాన్యత
అభ్యర్థన ఫ్రీక్వెన్సీ ప్రకారం మేము SKUలకు ప్రాధాన్యత ఇస్తాము:
def load_popular_skus() -> List[str]:
"""Load popular SKUs from access logs."""
try:
# ... (implementation details omitted)
జనరేషన్ వ్యూహం
def batch_generate():
# Load hash cache
hash_cache = load_hash_cache()
# ... (implementation details omitted)
షెడ్యూలింగ్
బ్యాచ్ జనరేషన్ cron ద్వారా ప్రతి రాత్రి నడుస్తుంది:
0 2 * * * cd /home/ubuntu/manage && python scripts/utils/generate_datasheets.py
నిల్వ
లోకల్: /home/ubuntu/web-static/ds/<sku>.pdf
S3: s3://thinvent-web-static/ds/<sku>.pdf
CDN: CloudFront ద్వారా అందించబడుతుంది
టెంప్లేట్ నిర్మాణం
డేటాశీట్ టెంప్లేట్ 3-నిలువు వరుసల లేఅవుట్ను ఉపయోగిస్తుంది:
<div class="container">
<!-- Header with logo and product name -->
<div class="header">
<img src="/s/logo.png">
<h1>{{ name }}</h1>
</div>
<!-- Main image -->
<img src="{{ images[0][1] }}" style="width: 9.5cm">
<!-- Thumbnails (up to 4) -->
<div class="thumbnails">
{% for name, url in images[1:5] %}
<img src="{{ url }}" style="width: 4.5cm">
{% endfor %}
</div>
<!-- Features in 3 columns -->
<div class="features">
<div class="column">
{% for heading, items in features1 %}
<h3>{{ heading }}</h3>
{% for name, value in items.items() %}
<div><strong>{{ name }}:</strong> {{ value }}</div>
{% endfor %}
{% endfor %}
</div>
<div class="column">
<!-- features2 -->
</div>
<div class="column">
<!-- features3 -->
</div>
</div>
<!-- Spacer to push footer down -->
<div style="height: {{ spacer_height }}cm"></div>
<!-- Footer with contact info -->
<div class="footer">
<p>www.thinvent.in | sales@thinvent.in | +91-124-4343177</p>
</div>
</div>
కాన్ఫిగరేషన్
లేఅవుట్ను స్థిరాంకాలు నియంత్రిస్తాయి:
DATASHEET_COLUMN_COUNT = 3
DATASHEET_HEADING_LINE_WEIGHT = 2 # Lines per heading
DATASHEET_FEATURE_NAME_MAX_LEN = 30 # Chars before wrapping
DATASHEET_FEATURE_VALUE_CHARS_PER_LINE = 40 # Chars per line
PDF_PAGE_SIZE = "A4"
ఇంటిగ్రేషన్ పాయింట్లు
ఉత్పత్తి పేజీలు
డేటాశీట్కు లింక్:
<a href="/ds/{{ sku }}.pdf" target="_blank">Download Datasheet</a>
Google Shopping
ఉత్పత్తి ఫీడ్లో డేటాశీట్లు లింక్ చేయబడతాయి:
<g:product_detail>
<g:section_name>Datasheet</g:section_name>
<g:attribute_name>PDF</g:attribute_name>
<g:attribute_value>https://www.thinvent.in/ds/{{ sku }}.pdf</g:attribute_value>
</g:product_detail>
ఇమెయిల్ ప్రచారాలు
కోట్ ఇమెయిల్లకు డేటాశీట్లు జోడించబడతాయి.
ఎర్రర్ హ్యాండ్లింగ్
చెల్లని SKU
if not check_sku(sku):
abort(422, description="Product not found.")
జనరేషన్ వైఫల్యం
try:
pdf_data = generate_datasheet_pdf(sku)
if pdf_data is None:
abort(500, description="PDF generation failed.")
except Exception as e:
logger.error(f"PDF generation failed for {sku}: {e}")
abort(500, description="PDF generation failed.")
చిత్రాలు లేకపోవడం
try:
with Image.open(image_path) as img:
width, height = img.size
except Exception:
# Use default dimensions
width, height = 800, 600
సూచనలు
లైబ్రరీలు
సంబంధిత వ్యాసాలు
-
ఫీచర్ ఎక్స్ట్రాక్షన్ - ఉత్పత్తి వివరాలను పొందడం
-
కంటెంట్ AI జనరేషన్ - ఉత్పత్తి వివరణలు
-
SKU నిర్మాణం - ఉత్పత్తి ఐడెంటిఫైయర్లు
సారాంశం
డేటాశీట్ జనరేషన్ హైబ్రిడ్ విధానాన్ని ఉపయోగిస్తుంది:
ఆన్-డిమాండ్:
-
వినియోగదారు అభ్యర్థించినప్పుడు సృష్టించండి
-
ఎల్లప్పుడూ తాజాగా ఉంటుంది
-
నిల్వ వృథా లేదు
-
~2 సెకన్లు ప్రతి PDFకి
బ్యాచ్:
-
ప్రసిద్ధ ఉత్పత్తులను ముందుగానే సృష్టించండి
-
productdb మార్పులను ట్రాక్ చేయండి
-
అభ్యర్థన ఫ్రీక్వెన్సీ ప్రకారం ప్రాధాన్యత ఇవ్వండి
-
ప్రతి రాత్రి cron జాబ్
ప్రక్రియ:
-
ఉత్పత్తి డేటాను పొందండి (పేరు, చిత్రాలు, ఫీచర్లు, వివరణ)
-
3 నిలువు వరుసలలో ఫీచర్లను సమతుల్యం చేయండి
-
చిత్ర ఎత్తులను లెక్కించండి
-
HTML టెంప్లేట్ను రెండర్ చేయండి
-
pdfkit తో PDFని సృష్టించండి
-
మొదటి పేజీని మాత్రమే ఉంచండి
నిల్వ:
-
లోకల్:
/home/ubuntu/web-static/ds/ -
S3:
s3://thinvent-web-static/ds/ -
CDN: CloudFront పంపిణీ
ఈ హైబ్రిడ్ విధానం తాజాదనం (ఆన్-డిమాండ్) మరియు పనితీరు (బ్యాచ్ ముందస్తు జనరేషన్) మధ్య సమతుల్యతను సాధిస్తుంది.