Compress this pdf but keep the information same and compress the pdf into less pages for quick revision
cd /home/daytona/workspace/attachments/04ab594a-ec34-4094-bd9f-3136386fb21e/ && python3 -c " import pdfplumber with pdfplumber.open('928b0382-636a-4791-893d-47f92b382ba2.pdf') as pdf: print(f'Total pages: {len(pdf.pages)}') for i, page in enumerate(pdf.pages[:5]): print(f'\n--- PAGE {i+1} ---') text = page.extract_text() if text: print(text[:800]) "
python3 -c " import pdfplumber import json all_text = [] with pdfplumber.open('/home/daytona/workspace/attachments/04ab594a-ec34-4094-bd9f-3136386fb21e/928b0382-636a-4791-893d-47f92b382ba2.pdf') as pdf: for i, page in enumerate(pdf.pages): text = page.extract_text() if text: all_text.append({'page': i+1, 'text': text}) with open('/home/daytona/workspace/pathology_text.json', 'w') as f: json.dump(all_text, f) # Also save as plain text with open('/home/daytona/workspace/pathology_full.txt', 'w') as f: for item in all_text: f.write(item['text'] + '\n\n') print(f'Extracted {len(all_text)} pages') total_chars = sum(len(p[\"text\"]) for p in all_text) print(f'Total characters: {total_chars}') "
python3 << 'PYEOF' import json import re from reportlab.lib.pagesizes import A4 from reportlab.lib.units import mm from reportlab.lib import colors from reportlab.platypus import ( SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, HRFlowable, KeepTogether ) from reportlab.platypus.flowables import Flowable from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_JUSTIFY from reportlab.platypus import Frame, PageTemplate, BaseDocTemplate from reportlab.platypus.doctemplate import _doNothing # Load extracted text with open('/home/daytona/workspace/pathology_full.txt', 'r') as f: full_text = f.read() # ---- Custom 2-column doc ---- PAGE_W, PAGE_H = A4 MARGIN = 10 * mm COL_GAP = 5 * mm COL_W = (PAGE_W - 2 * MARGIN - COL_GAP) / 2 def make_frames(page_w, page_h, margin, col_gap, col_w): frame1 = Frame(margin, margin, col_w, page_h - 2*margin, leftPadding=0, rightPadding=2, topPadding=0, bottomPadding=0, id='col1') frame2 = Frame(margin + col_w + col_gap, margin, col_w, page_h - 2*margin, leftPadding=2, rightPadding=0, topPadding=0, bottomPadding=0, id='col2') return [frame1, frame2] class TwoColumnDoc(BaseDocTemplate): def __init__(self, filename, **kwargs): BaseDocTemplate.__init__(self, filename, **kwargs) frames = make_frames(PAGE_W, PAGE_H, MARGIN, COL_GAP, COL_W) template = PageTemplate(id='TwoCol', frames=frames, onPage=self.add_page_number) self.addPageTemplates([template]) def add_page_number(self, canvas, doc): canvas.saveState() canvas.setFont('Helvetica', 6) canvas.setFillColor(colors.grey) canvas.drawRightString(PAGE_W - MARGIN, MARGIN - 4*mm, f"Page {doc.page}") canvas.restoreState() # ---- Styles ---- styles = getSampleStyleSheet() title_style = ParagraphStyle('DocTitle', fontName='Helvetica-Bold', fontSize=11, leading=13, textColor=colors.HexColor('#1a1a6e'), alignment=TA_CENTER, spaceAfter=3) h1_style = ParagraphStyle('H1', fontName='Helvetica-Bold', fontSize=7.5, leading=9, textColor=colors.HexColor('#1a1a6e'), spaceBefore=4, spaceAfter=1, borderPad=1) h2_style = ParagraphStyle('H2', fontName='Helvetica-Bold', fontSize=7, leading=8.5, textColor=colors.HexColor('#0055aa'), spaceBefore=3, spaceAfter=1) h3_style = ParagraphStyle('H3', fontName='Helvetica-BoldOblique', fontSize=6.5, leading=8, textColor=colors.HexColor('#006600'), spaceBefore=2, spaceAfter=0.5) body_style = ParagraphStyle('Body', fontName='Helvetica', fontSize=6, leading=7.5, textColor=colors.black, spaceBefore=0, spaceAfter=0.5, alignment=TA_JUSTIFY) bullet_style = ParagraphStyle('Bullet', fontName='Helvetica', fontSize=6, leading=7.5, textColor=colors.black, leftIndent=8, firstLineIndent=-5, spaceBefore=0, spaceAfter=0) sub_bullet_style = ParagraphStyle('SubBullet', fontName='Helvetica', fontSize=5.8, leading=7, textColor=colors.HexColor('#333333'), leftIndent=14, firstLineIndent=-5, spaceBefore=0, spaceAfter=0) hr_style = ParagraphStyle('HR', fontName='Helvetica', fontSize=1, leading=1, spaceBefore=2, spaceAfter=2) # ---- Parse and convert text to flowables ---- def escape_xml(text): text = text.replace('&', '&') text = text.replace('<', '<') text = text.replace('>', '>') return text def bold_inline(text): """Convert **text** to <b>text</b>""" text = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', text) text = re.sub(r'\*(.*?)\*', r'<i>\1</i>', text) return text def parse_table(lines, start_idx): """Parse markdown table lines into a list of rows.""" rows = [] i = start_idx while i < len(lines): line = lines[i].strip() if line.startswith('|') and line.endswith('|'): if re.match(r'^\|[-| :]+\|$', line): i += 1 continue cells = [c.strip() for c in line[1:-1].split('|')] rows.append(cells) else: break i += 1 return rows, i def make_table_flowable(rows): if not rows: return None # Make all rows same number of cols max_cols = max(len(r) for r in rows) padded = [r + [''] * (max_cols - len(r)) for r in rows] table_data = [] for ri, row in enumerate(padded): cell_paras = [] for cell in row: cell_text = escape_xml(cell) cell_text = bold_inline(cell_text) style = ParagraphStyle('tcell', fontName='Helvetica-Bold' if ri == 0 else 'Helvetica', fontSize=5.2, leading=6.5, textColor=colors.white if ri == 0 else colors.black) cell_paras.append(Paragraph(cell_text, style)) table_data.append(cell_paras) col_width = (COL_W - 4) / max_cols t = Table(table_data, colWidths=[col_width]*max_cols, repeatRows=1) t.setStyle(TableStyle([ ('BACKGROUND', (0,0), (-1,0), colors.HexColor('#1a1a6e')), ('BACKGROUND', (0,1), (-1,-1), colors.HexColor('#f0f4ff')), ('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.HexColor('#f0f4ff'), colors.HexColor('#e4eeff')]), ('GRID', (0,0), (-1,-1), 0.3, colors.HexColor('#aaaacc')), ('VALIGN', (0,0), (-1,-1), 'TOP'), ('TOPPADDING', (0,0), (-1,-1), 1), ('BOTTOMPADDING', (0,0), (-1,-1), 1), ('LEFTPADDING', (0,0), (-1,-1), 2), ('RIGHTPADDING', (0,0), (-1,-1), 2), ])) return t def text_to_flowables(text): flowables = [] lines = text.split('\n') i = 0 while i < len(lines): line = lines[i] stripped = line.strip() # Skip empty lines if not stripped: i += 1 continue # Markdown headings if stripped.startswith('# ') and not stripped.startswith('## '): content = escape_xml(stripped[2:].strip()) content = bold_inline(content) flowables.append(HRFlowable(width='100%', thickness=0.5, color=colors.HexColor('#1a1a6e'), spaceAfter=1, spaceBefore=3)) flowables.append(Paragraph(content, h1_style)) i += 1 continue if stripped.startswith('## ') and not stripped.startswith('### '): content = escape_xml(stripped[3:].strip()) content = bold_inline(content) flowables.append(Paragraph(content, h2_style)) i += 1 continue if stripped.startswith('### '): content = escape_xml(stripped[4:].strip()) content = bold_inline(content) flowables.append(Paragraph(content, h3_style)) i += 1 continue # Markdown table if stripped.startswith('|') and stripped.endswith('|'): rows, i = parse_table(lines, i) tbl = make_table_flowable(rows) if tbl: flowables.append(Spacer(1, 1)) flowables.append(tbl) flowables.append(Spacer(1, 1)) continue # Horizontal rule if stripped in ('---', '***', '___'): flowables.append(HRFlowable(width='100%', thickness=0.3, color=colors.HexColor('#aaaaaa'), spaceAfter=1, spaceBefore=1)) i += 1 continue # Bullet points if stripped.startswith('- ') or stripped.startswith('* '): content = escape_xml(stripped[2:].strip()) content = bold_inline(content) flowables.append(Paragraph(u'\u2022 ' + content, bullet_style)) i += 1 continue if re.match(r'^\s{2,}[-*] ', line): content = escape_xml(stripped[2:].strip()) content = bold_inline(content) flowables.append(Paragraph(u' \u25e6 ' + content, sub_bullet_style)) i += 1 continue # Numbered list if re.match(r'^\d+\. ', stripped): content = escape_xml(stripped) content = bold_inline(content) flowables.append(Paragraph(content, bullet_style)) i += 1 continue # Regular paragraph content = escape_xml(stripped) content = bold_inline(content) flowables.append(Paragraph(content, body_style)) i += 1 return flowables # ---- Build document ---- output_path = '/home/daytona/workspace/pathology_compressed.pdf' doc = TwoColumnDoc(output_path, pagesize=A4, leftMargin=MARGIN, rightMargin=MARGIN, topMargin=MARGIN, bottomMargin=MARGIN + 4*mm, title='Pathology Paper I - Compressed Revision Notes', author='Quick Revision') story = [] # Title page header story.append(Paragraph('PATHOLOGY PAPER I', ParagraphStyle('MainTitle', fontName='Helvetica-Bold', fontSize=13, leading=15, textColor=colors.HexColor('#1a1a6e'), alignment=TA_CENTER, spaceBefore=0, spaceAfter=1))) story.append(Paragraph('COMPLETE REVISION NOTES (Robbins & Kumar)', ParagraphStyle('SubTitle', fontName='Helvetica-Oblique', fontSize=7, leading=9, textColor=colors.HexColor('#555555'), alignment=TA_CENTER, spaceAfter=4))) story.append(HRFlowable(width='100%', thickness=1, color=colors.HexColor('#1a1a6e'), spaceAfter=3)) # Parse and add content story.extend(text_to_flowables(full_text)) doc.build(story) print("PDF built successfully at:", output_path) # Check page count from pypdf import PdfReader r = PdfReader(output_path) print(f"Compressed PDF pages: {len(r.pages)}") PYEOF
ls -lh /home/daytona/workspace/pathology_compressed.pdf && python3 -c " from pypdf import PdfReader r = PdfReader('/home/daytona/workspace/pathology_compressed.pdf') print(f'Pages: {len(r.pages)}') # Check text is present t = r.pages[0].extract_text() print('Page 1 sample:', t[:300]) "
| Original | Compressed | |
|---|---|---|
| Pages | 54 | 7 |
| File size | ~original | 69 KB |
| Reduction | - | 87% fewer pages |
pathology compressed
PDF Document · PDF
Answer all questions from harsh mohan for professional exam and compress the pdf but keep the information same and compress the pdf into less pages for quick revion
python3 -c " import pdfplumber with pdfplumber.open('/home/daytona/workspace/attachments/04ab594a-ec34-4094-bd9f-3136386fb21e/Pathology_Paper_2.pdf') as pdf: print(f'Total pages: {len(pdf.pages)}') for i, page in enumerate(pdf.pages[:8]): print(f'\n--- PAGE {i+1} ---') text = page.extract_text() if text: print(text[:1000]) "
python3 -c " import pdfplumber with pdfplumber.open('/home/daytona/workspace/attachments/04ab594a-ec34-4094-bd9f-3136386fb21e/Pathology_Paper_2.pdf') as pdf: full = '' for page in pdf.pages: t = page.extract_text() if t: full += t + '\n' print(full) "
Answer all question from harsh mohan bo ok for professional exam
python3 -c " import pdfplumber with pdfplumber.open('/home/daytona/workspace/attachments/04ab594a-ec34-4094-bd9f-3136386fb21e/Pathology_Paper_2 (1).pdf') as pdf: print(f'Total pages: {len(pdf.pages)}') full = '' for page in pdf.pages: t = page.extract_text() if t: full += t + '\n' print(full) "