[Tex/LaTex] Alignment problem with fancyhdr for a report page layout

fancyhdrheader-footervertical alignment

I am trying to define a base class for some reports to share the same page layout. I use the fancyhdr package for creating the header and footer but I am experiencing some vertical alignment problems. I spent hours trying to figure out how to do what seems so simple in HTML. In the following images, I used the package showframe to help identifying the page layout.

Here is an image of what I would like to achieve:

![report base image]1

Here is an image of what I have right now:

![report base result]2

Apart from the general page layout, I cannot vertically align the report name and date to the top with the image:

![report header alignment]3

The page body overlaps the header:

![report body overlaps]4

The page footer overlaps the body:

![report footer overlaps]5

Finally, here is the Latex code used for the class:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{basereport}

% base class
\LoadClass[letterpaper, 12pt]{article}

% packages
\usepackage[utf8]{inputenc} % The UTF-8 encoding is specified.
\usepackage{color}
\usepackage{lipsum}
\usepackage{helvet}
\usepackage{graphicx}
\usepackage{fancyhdr} % for header and footer
\usepackage{lastpage}
\usepackage[yyyymmdd,hhmmss]{datetime}

% debug frames
\usepackage{showframe}

% margins
% http://ctan.mirror.rafal.ca/macros/latex/contrib/geometry/geometry.pdf
\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm
]{geometry}

% macro for images
\DeclareGraphicsExtensions{.pdf,.png,.jpg}

% page style
% http://ctan.bppro.ca/macros/latex/contrib/fancyhdr/fancyhdr.pdf
\pagestyle{fancy} % for page style

% page header
\fancyhf{} % reset header and footer
\renewcommand{\headrulewidth}{0pt} % remove header rule
\renewcommand{\footrulewidth}{0pt} % remove footer rule

\fancyhead[L]{
    \includegraphics[width=4cm, height=2cm, keepaspectratio=true]{universe.jpg}
}
\fancyhead[C]{}
\fancyhead[R]{
    \large\textbf{
        \MakeUppercase{Report name}
        \linebreak
        2015-11-10
    }
}

% page footer
\fancyfoot[L]{
    \small\emph{Printed on \today\ at \currenttime}
}
\fancyfoot[C]{}
\fancyfoot[R]{
    \small\emph{Page \thepage\ of \pageref{LastPage}}
}

% commands
\renewcommand{\familydefault}{\sfdefault}

and the document:

\documentclass{basereport}
\begin{document}
\textcolor[rgb]{0,0,1}{
    \lipsum[1]
}
\end{document}

I tried to play with the length of those properties:

\hoffset
\voffset
\headheight
\textheight
\textwidth
\footskip
\headsep

EDIT: Here is what I got for using some of those properties:

\setlength{\hoffset}{0cm}
\setlength{\voffset}{1cm}

\setlength{\headheight}{2cm}
\setlength{\headsep}{0.5cm}
\setlength{\footskip}{0.5cm}

report base result with properties

It seems like the geometry package is being overridden by fancyheader? I am not sure to understand the conflicts. If I ask for 1cm margins, why do I have the set voffset? Also, is there any way of specifying the header and footer heights, and let the body use the remainder of the document without having to set textheight? How to vertical align the text in the header to the top?

EDIT 2:

I set the properties in the geometry package declaration instead of manually as suggested and it helped a lot. I am still usure why the top value must include the headheight and headsep plus the desired margin value.

report base result with geometry

Two questions remains:

1) how to vertically align my header so the title and date are at the top?

2) how to remove the line under the footer which appears even if footrulewidth is set to 0.0pt?

The updated code of the class for reference:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{basereport}

% base class
\LoadClass[letterpaper, 12pt]{article}

% packages
\RequirePackage[utf8]{inputenc} % The UTF-8 encoding is specified.
\RequirePackage{color}
\RequirePackage{lipsum}
\RequirePackage{helvet}
\RequirePackage{graphicx}
\RequirePackage{fancyhdr} % for header and footer
\RequirePackage{lastpage}
\RequirePackage[yyyymmdd,hhmmss]{datetime}

% debug frames
\RequirePackage{showframe}

% margins
% http://ctan.mirror.rafal.ca/macros/latex/contrib/geometry/geometry.pdf
\RequirePackage[
    headheight=30mm,
    headsep=5mm,
    footskip=10mm,
    top=45mm, 
    bottom=20mm, 
    left=10mm, 
    right=10mm
]{geometry}

% macro for images
\DeclareGraphicsExtensions{.pdf,.png,.jpg}

% page style
% http://ctan.bppro.ca/macros/latex/contrib/fancyhdr/fancyhdr.pdf
\pagestyle{fancy} % for page style

% page header
\fancyhf{} % reset header and footer
\renewcommand{\headrulewidth}{0pt} % remove header rule
\renewcommand{\footrulewidth}{0pt} % remove footer rule

\fancyhead[L]{
    \includegraphics[keepaspectratio=true]{universe.jpg}
}
\fancyhead[C]{}
\fancyhead[R]{
    \large\textbf{
        \MakeUppercase{Report name}
        \linebreak
        2015-11-10
    }
}

% page footer
\fancyfoot[L]{
    \small\emph{Printed on \today\ at \currenttime}
}
\fancyfoot[C]{}
\fancyfoot[R]{
    \small\emph{Page \thepage\ of \pageref{LastPage}}
}

% commands
\renewcommand{\familydefault}{\sfdefault}

Best Answer

The basic error is that the \headheight length is too small, this leads to an overlapping at the top of the page header and the regular text.

fancyhdr will issue a warning and some recommendation about correct values on the console and in the log file.

Use \setlength{\headheight}{2cm} (or whatever appropiate value is reported by fancyhdr) or use \usepackage[headheight=2cm,...]{geometry}.

Related Question